I thought I knew how to declare variables until I just saw this example.
In the example a constant is declared.
let timeOfDay: String
I thought you could not declare a variable/constant without initializing it unless you declared it as an Optional?
Of course the program bombs when I go to print the constant. How is it the compiler allowed this? I entered this into a Playground.
A full example:
let hourOfDay = 12
let timeOfDay: String
if hourOfDay < 6 {
timeOfDay = "Early morning"
} else if hourOfDay < 12 {
timeOfDay = "Morning"
} else if hourOfDay < 17 {
timeOfDay = "Afternoon"
} else if hourOfDay < 20 {
timeOfDay = "Evening"
} else if hourOfDay < 24 {
timeOfDay = "Late evening"
} else {
timeOfDay = "INVALID HOUR!"
}
print(timeOfDay)
timeOfDayis declared.