original articles, welcome reprint. Reprint Please specify: Dongsheng's blog
When you use optional types and optional chains, you use the question mark multiple times (? ) and an exclamation point ( ! ), but their meaning is different, let me explain in detail below.
1. question mark (?) in the optional type. )
declares that this type is an optional type and uses an exclamation mark when accessing a variable or constant of this type ( ! ), the following code shows the split package:
Let result1 : Double? = Divide (+)
Print (result1!)
2. an exclamation mark in an optional type (! )
declaring this type is optional, but you can access variables or constants of this type without using an exclamation point ( ! ), the following code is an implicit unpacking:
Let result3 : Double ! = Divide (+)
Print (RESULT3)
3. an exclamation point in the optional chain (! )
Multiple instances have an association, and a reference chain is formed when a member of another instance's methods, properties, and subscripts are referenced from one instance, and since some links in this "chain" may have values or no values, they need to be accessed in the following way:
Emp.dept!.comp!.name
4. question mark (?) in the optional chain. )
use an exclamation mark in an optional chain ( ! ), once the "chain" has no value, the program will be abnormal, we put the exclamation mark ( ! ) Instead of a question mark ( ? ), the code looks like this:
Emp.dept?.comp?.name
so that some links do not have value when the return Nil , the program does not have an exception.
Welcome to follow Dongsheng Sina Weibo@tony_Dongsheng.
Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom
More ProductsIOS,Cocos, mobile Design course please pay attention to the official website of Chi Jie Classroom:http://www.zhijieketang.com
Smart-Jie Classroom Forum Website:http://51work6.com/forum.php
Swift 2.0 Study Notes (Day 28)-summarize the use of question marks (?) and exclamation points (!)