IOS Development Diary (5)——? with! usage of

Optional type (Optional): It may be nil or a specific value

1. Declare an optional type

var temp:String?

temp="test"

 

 

2. Use optional types of variables

print(temp) //The output result is: Optional("test")

print(temp!) //The output result is: test

 

Summary: add after the type? Indicates an optional type, add after the variable! Indicates that specific values ​​can be obtained by unpacking optional types

 

3. Optional chain

Shaped like: person?.book!.price! or person.book!.price! 

If book is nil, the second will report an error, and the first will return an optional type to continue execution

 

4. Hermit unpacking

形如:@IBOutlet weak var tableView: UITableView!  

Hermit unpacking is equivalent to automatically adding after the variable every time you use it! Unpack

 

Summary: add after the variable? Indicates an optional chain, add after the type! Means hermit unpacking

 

Guess you like

Origin blog.csdn.net/hzkcsdnmm/article/details/106850890