Learning a burst of swift for the optional type has been not very understanding, for this purpose to learn a bit, by watching the Stanford University video, the summary of what he said
Optional has two values
1.not set (not set)
Represents this value has not been set by anyone, or someone has set him up as an undefined state
We say this is nil, that is, the meaning is not set, that is, the value of the optional variable is not set
2.something (assigned a value)
Let digit:string?
is set to assign a value, what is the type of this value? The type that the preceding thing represents
If the string to the left of the question mark indicates that if the optional variable is assigned a value, then its type is string and we can call it optional string.
He's really a optional type, he's not a nil string, but he's a optional type. can be assigned to string
Unwrapped (unpacking) This optional type of data is to open this optional type of data to find the value inside it
Use! You can get the value to it.
There are two scenarios for unpacking:
If the optional data is not set to nil, we unpack unwrapped the program crashes,
If it's not nil, you get a string or other type of data.
Swift Optional Detailed