The constant variable songName is non-optional. It is valid only in the if block iff favoriteSong has valid data.
The non-optional songName cannot be used elsewhere.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
if let songName = favoriteSong { //songName can only be accessed here print("Favorite song is: " + songName) } else { print("I don't know what your favorite song is!") //error, cannot use outside of if/else scope print("songName - \(songName)") } //error, cannot use outside of if/else scope print("songName - \(songName)") |