Anonymous user
Null object: Difference between revisions
→{{header|Swift}}: Update Swift entry to modern syntax and add another example
(→{{header|Swift}}: Update Swift entry to modern syntax and add another example) |
|||
Line 1,671:
=={{header|Swift}}==
Swift has <code>Optional<T></code> type, where <code>nil</code> means a lack of value.
<code>T?</code> is syntactic sugar for <code>Optional<T></code>.
<lang swift>let maybeInt: Int? = nil</lang>
To
<lang swift>if
} else {
}</lang>
Usually you want to access the value after checking if it's nil. To do that you use <code>if let</code>
<lang swift>if let certainlyInt = maybeInt {
print("variable has value \(certainlyInt)")
} else {
print("variable is nil")
}</lang>
|