var morseText = ".... . -.-- - .... . .-. ."
In English, it is "hey there"
var morseCodeArray: [String] = []
This is the array where individual morse code letters will be stored.
var currMorse: String = ""
currMorse is used to keep track of the current Morse letter.
for char in morseText {
if char != " " {
currMorse.append(char)
} else {
switch currMorse {
case "":
currMorse += " "
case " ":
morseCodeArray.append(" ")
currMorse = ""
default:
morseCodeArray.append(currMorse)
currMorse = ""
}
}
}
morseCodeArray.append(currMorse)
Could someone please explain what exactly is happening in the switch statement?
I know Apple wants switch statements to be exhaustive, and hence we have three cases here.
What I do not understand is the purpose of case #1 i.e. case "":
Why do we need this null/nil option? And even if we do, why do we then need to do this -> currMorse += " "
My programming-novice brain thinks we only need case #2 i.e. case " ":
Because all the other char options (like dots, dashes in morseText) will be covered by the if statement