Programming in Swift: Fundamentals · Optionals | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5539282-programming-in-swift-fundamentals/lessons/7

At about 4:00 when result is set to Int? = 30, why make Int an optional when you know it has a value of 30?

Declaring a variable as an Optional Int (like var result: Int? = 30) means that you are free to assign any other value, including nil (i.e., no value), to that variable in the future.

If you only declared it as var result: Int = 30, then you wouldn’t have the option later on to assign nil to the variable result, should the need arise.

8:15
The line 18-20 is not the same as line 22. In line 19, unwrappedPetName is still an optional.
It’s the same, if you use:
if petName != nil {
let unwrappedPetName = petName!
}

@hvonweg Thank you for sharing your solution - much appreciated!

I just wanted to shoutout the Doom fan that threw a cacodemon in <3

1 Like

You can thank the original creator of the series, @catie for that!

hahahahahah. I like the last part where @chrisrazeware acts all confused about the optional challenge.

You also get +100 points for that awesome username @commodore64 #C64forLife