Optional values of swift (optional) and swiftoptional
Optional value: optional
Unlike OC, swift introduces a new concept-optional value. To understand this optional, it is crucial for swift to learn.
Concept definition: A type with either a value or no value (nil. In other words, it has two states: value, no value (nil)
In my understanding, the optional value is a normal type with a boolean type.
How to define an optional value?
The code above briefly defines an optional value called optionalValue.
There is no big difference from the definition of common variables. The only difference is that there is a question mark ,? Is to add the normal value of the packet after the type, that is, add a question mark after the type of the variable or constant to define it as an optional
Here I assign 80 to it. If no value is assigned, it will be nil by default.
Optional value force unpack forced unwrapping
By adding a question mark after the type, you can change this variable or constant to an optional value. How can this problem be solved in turn?
Swift provides an understanding of the package operation. You only need to add an exclamation mark after the optional value to force unpack the optional value.
It is said that a thing with a mandatory nature is not a good thing, so the problem is ......
In the above code, I created an optional variable without a value assignment. The default value is nil.
In the second row, an error occurs when I try to unpackage the optional.
It turns out that an error will occur when the nil optional value is unwrapped. Apple's official document also emphasizes that if you are not 100% sure that this optional value has a value, do not use force unwrapped.
Optional value: Optional Binding
Since an error is reported when the optional value is nil, how can we read the data in the optional value?
Swift provides a safer method -- Binding Optional values to Optional Binding
The usage provided in the above official documents is not clear. The following is a code Demonstration:
In the code above, I first defined an optional value string, assigned a value of 90, and then used optional binding to create a constant called tempValue, the if statement determines whether to print the value based on whether the tempValue is obtained from optionalValue. The playground result shows that the tempValue is assigned "90"
When I do not assign a value to the optional value and set it to nil by default, no error is reported when the tempValue is printed. if the tempValue is determined not to be obtained from the optionValue, nothing is done, of course, no error is reported.
This is equivalent to finding a man-in-the-middle for the optional value, indirectly judging and reading the content in the optional value, and not sure whether the current value is nil, apple officially recommends using this method to determine whether the optional value is nil and try to obtain the data of the optional value.
For more information, visit the http://www.cnblogs.com/updating/ reprinted please indicate the source