When we use oc development, we often encounter null values. If no judgment is added, the program may crash. Swift language adds the concept of optional values. The basic syntax is as follows:
Optional types:
var optValue: Int? = 8
Add one after the type? It means that the value of optValue can be nil, "?" Is a mark of optional value,? Is actually a type of Optionals, we can look at the source code of swift:
enum Optional <T>: Reflectable, NilLiteralConvertible {
case None
case Some (T)
/// Construct a `nil` instance.
init ()
/// Construct a non- \ `nil` instance that stores` some`.
init (_ some: T)
/// Returns a mirror that reflects `self`.
func getMirror ()-> MirrorType
/// Create an instance initialized with `nil`.
init (nilLiteral: ())
}
It can be seen from the source code that the Optionals type is actually an enumerated type, one is none, and the other is non-none:
Because optValue is an optional value, you can assign nil to optValue, see the following code:
var optValue: Int? = 8
optValue = nil
let optValue2 = optValue
println (optValue2)
At this time, optValue is nil. When assigning it to optValue2, the program will crash, but you can use the following methods:
var optValue: Int? = 8
optValue = nil
if let t = optValue {
println ("\ (t)")
} else {
println ("nil")
}
If optValue is nil, then the assignment statement will not be executed, and the program will print nil, which is optional binding.
When it is necessary to unpack optValue, add one after the variable!
such as:
var optValue: Int? = 8
var t: Int = optValue!
println (t)
At this time, the value of type int in optValue will be unpacked and assigned to t, and the printed value is 8;
Implicit parsing optional
Sometimes in the program architecture, after the first assignment, it can be determined that an optional will always have a value. In this case, it is very inefficient to judge and parse the optional value every time, because it can be determined that it will always have a value.
This type of optionality is defined as implicitly unwrapped optionals. Declare an implicit parsing optional by changing the question mark (String?) Behind the type you want to use as an optional type (String!)
Implicit parsing of optionals is very useful when optionals can be determined after they have been assigned for the first time.
let optValue: Int! = 8
if optValue! = nil {
println (optValue)
}
if let t = optValue {
println (t)
}
var tm: Int = optValue
println (tm)
You can think of implicit parsing optional as an automatic parsing optional. All you have to do is put the exclamation mark at the end of the type when declaring, not at the end of the optional name that is evaluated each time.
Note: If you try to get a value while implicitly parsing the optional no value, a runtime error will be triggered. It is the same as if you add an exclamation mark after the ordinary option with no value.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.