《浅入浅出Swift》Optional Chaining 可选链

####Optional Chaining

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

//可选型是!?

struct MyName{
var name:String = "nicai"
}
struct MyInfo{
var myName : MyName? = MyName()
}
class MyClass{
var structInstance:MyInfo? = MyInfo()
}
var myInstance = MyClass()
println("myInstance = \(myInstance)")

//使用一连串的可选值,叫做可选链
//在可选链中,任何一个环节解析失败(nil),那么整个可选链的结果为nil
if let name = myInstance.structInstance?.myName?.name{
println(name)
}else{
println("nil")
}

Reference

坚持原创技术分享,您的支持将鼓励我继续创作!
0%