Group Group Group Group Group Group Group Group Group

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!
}