查询,调用属性,下标和方法上的一个可选可能 'nil' 的过程被定义为可选的链。可选链返回两个值
如果可选包含一个值,然后调用其相关属性,方法和下标返回值
如果可选包含一个“nil”值,所有的相关属性,方法和下标返回nil
由于多种查询方法,属性和下标故障组合在一起,以一种链将影响到整个链,并导致产生 'nil' 的值。
可选链作为一种替代强制解包裹
可选链与可选值后指定“?”调用一个属性,方法或下标当可选的值返回一些值。
程序用于可选链 '!'
class ElectionPoll {
var candidate: Pollbooth?
}
class Pollbooth {
var name = "MP"
}
let cand = ElectionPoll()
let candname = cand.candidate!.name
</div>
当我们使用 playground 运行上面的程序,得到以下结果。
fatal error: unexpectedly found nil while unwrapping an Optional value 0 swift 0x0000000103410b68 llvm::sys::PrintStackTrace(__sFILE*) + 40 1 swift 0x0000000103411054 SignalHandler(int) + 452 2 libsystem_platform.dylib 0x00007fff9176af1a _sigtramp + 26 3 libsystem_platform.dylib 0x000000000000000b _sigtramp + 1854492939 4 libsystem_platform.dylib 0x00000001074a0214 _sigtramp + 1976783636 5 swift 0x0000000102a85c39 llvm::JIT::runFunction(llvm::Function*, std::__1::vector > const&) + 329 6 swift 0x0000000102d320b3 llvm::ExecutionEngine::runFunctionAsMain(llvm::Function*, std::__1::vector, std::__1::allocator >, std::__1::allocator, std::__1::allocator > > > const&, char const* const*) + 1523 7 swift 0x000000010296e6ba swift::RunImmediately(swift::CompilerInstance&, std::__1::vector, std::__1::allocator >, std::__1::allocator, std::__1::allocator > > > const&, swift::IRGenOptions&, swift::SILOptions const&) + 1066 8 swift 0x000000010275764b frontend_main(llvm::ArrayRef, char const*, void*) + 5275 9 swift 0x0000000102754a6d main + 1677 10 libdyld.dylib 0x00007fff8bb9e5c9 start + 1 11 libdyld.dylib 0x000000000000000c start + 1950751300 Stack dump: 0. Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret - -target x86_64-apple-darwin14.0.0 -target-cpu core2 -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -module-name main /bin/sh: line 47: 15672 Done cat <<'SWIFT' import Foundation</div>
上述程序中声明“ election poll” 作为类名,并包含了作为隶属函数“candidate”。子类被声明为 “poll booth” 和 “name” 作为被初始化为 'MP' 的隶属度函数。对超类的调用是通过创建一个实例,“cand”可选的初始化 "!“。由于这些值在它的基类没有声明,“nil” 值被存储,从而通过强制解包处理过程返回一个致命的错误。
程序用于可选链 '?'
class ElectionPoll {
var candidate: Pollbooth?
}
class Pollbooth {
var name = "MP"
}
let cand = ElectionPoll()
if let candname = cand.candidate?.name {
println("Candidate name is \(candname)")
}
else {
println("Candidate name cannot be retreived")
}
</div>
当我们使用 playground 运行上面的程序,得到以下结果。
Candidate name cannot be retreived</div>
上述程序中声明“ election poll” 作为类名,并包含了作为隶属函数“candidate”。子类被声明为 “poll booth” 和 “name” 作为被初始化为 'MP' 的隶属度函数。对超类的调用是通过创建一个实例,“cand”可选的初始化“?”。由于基类 'nil' 的值不声明被存储并打印在控制台,由其他程序块处理。
定义模型类的可选链接和访问属性
Swift 语言还提供可选链的概念,声明多个子类的模型类。这个概念将是定义复杂的模型和访问属性,方法和下标子属性非常有用。
class rectangle {
var print: circle?
}
class circle {
var area = [radius]()
var cprint: Int {
return area.count
}
subscript(i: Int) -> radius {
get {
return area[i]
}
set {
area[i] = newValue
}
}
func circleprint() {
println("The number of rooms is \(cprint)")
}
var rectarea: circumference?
}
class radius {
let radiusname: String
init(radiusname: String) { self.radiusname = radiusname }
}
class circumference {
var circumName: String?
var circumNumber: String?
var street: String?
func buildingIdentifier() -> String? {
if circumName != nil {
return circumName
} else if circumNumber != nil {
return circumNumber
} else {
return nil
}
}
}
let rectname = rectangle()
if let rectarea = rectname.print?.cprint {
println("Area of rectangle is \(rectarea)")
} else {
println("Rectangle Area is not specified")
}
</div>
当我们使用 playground 运行上面的程序,得到以下结果。
Rectangle Area is not specified</div>
通过可选链调用方法
class rectangle {
var print: circle?
}
class circle {
var area = [radius]()
var cprint: Int {
return area.count
}
subscript(i: Int) -> radius {
get {
return area[i]
}
set {
area[i] = newValue
}
}
func circlep