Reputation: 5523
First off, I understand what an optional is and what it does in Swift. However, I noticed the absence of a technical definition for it in the Apple Swift documentation. It defines it as:
You use optionals in situations where a value may be absent. An optional says:
There is a value, and it equals x or
There isn’t a value at all
So...is optional a type? a type declaration? How would you actually define optional in one sentence?
Upvotes: 1
Views: 853
Reputation: 2854
The Swift language defines the postfix ? as syntactic sugar for the named type Optional, which is defined in the Swift standard library.
The type Optional is an enumeration with two cases, None and Some(T), which are used to represent values that may or may not be present. Any type can be explicitly declared to be (or implicitly converted to) an optional type. If you don’t provide an initial value when you declare an optional variable or property, its value automatically defaults to nil.
You can find more details in the official documentation
Upvotes: 2
Reputation: 12015
Optional is an Enum
, which can have two states:
.None
.Some(T)
Here are some more information about Optionals
.
Upvotes: 4