April 2017
Intermediate to advanced
316 pages
9h 33m
English
Swift provides optionals so they can be used in situations where a value may be absent. An optional will have some or none values. The ? symbol is used to define a variable as optional. Consider the following example:
// Optional value either contains a value or contains nil var optionalString: String? = "A String literal" optionalString = nil
The ! symbol can be used to forcefully unwrap the value from an optional. For instance, the following example forcefully unwraps the optionalString variable:
optionalString = "An optional String" print(optionalString!)
Force unwrapping the optionals may cause errors if the optional does not have a value, so it is not recommended to use this approach as it is very hard to be sure if we ...
Read now
Unlock full access