0%

避免强转

总得说点什么,🙂
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import Foundation

class Person {
var name = ""
}

class Teacher: Person {
var age = 0
}

let teacher: Person = Teacher()

// 强转使用
let age = (teacher as! Teacher).age

// 避免强转
if let tea = Optional(teacher) as? Teacher {
print(tea.age)
}

–EOF–