上述程序中声明“ 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")
}
当我们使用 playground 运行上面的程序,得到以下结果。
Candidate name cannot be retreived
上述程序中声明“ 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)")








