I believe I understand why optionals are handy (my best thought for use is to be able to return a nil Boolean value), but in what case would I want to declare a wrapped opti
Implicitly unwrapped optionals introduce a possible failure similar to dereferencing null pointers. You should only use them when you can be sure that no one will accidentally pass nil, or when you're comfortable telling clients of the interface that the function will crash if they violate the interface requirements.
Another way to avoid using ? Repeatedly is the if let statement:
func foo(bar:Int?) -> Int? {
if let x = bar {
if Int.max / x >= x {
return x * x
}
}
return nil
}
x
here is an Int
rather than Int?
.