struct
enum
class
All can conform to protocols and can be extended
var possibleInt: Int?
possibleInt!
- gives an Int
; blows up if there is no value
if possibleInt != nil {
print("The int is \(possibleInt!)")
}
if let theInt = possibleInt {
print("The int is \(theInt)")
}
Provides a default value when the optional is nil.
let name = possibleName ?? "hi"
For when an optional starts out without a value, but it’s clear from a program’s structure that an optional will always have a value after that value is first set. Useful especially in class initialization.
var assumedInt: Int!
assumedInt = 42
print(assumedInt)
var possibleString: String!
print(possibleString?.lowercased)
Different from forced unwrapping in that optional chaining fails gracefully and returns a nil
, whereas forced unwrapping triggers a runtime error.
print(optionalDate.map(dateFormatter.stringFromDate))
If optionalDate
is defined, calls dateFormatter.stringFromDate(optionalDate)
and returns the result; otherwise returns nil
.
control as? UIButtonView
Returns a UIButtonView?
that has no value if the type cast couldn’t occur.
control as! UIButtonView
Force unwraps and triggers a runtime error if the type cast couldn’t occur.
var someProperty: Type {
return /* some computed value*/
}
var someProperty: Type {
willSet {
// ...
}
didSet {
// ...
}
}