一、is
用来判断是否为某种类型
class Studetn : Person{}
var p = Person()
var s = Studetn()
if p is Person {
print("p iskiindf Person")
}else{
print("NO")
}
if s is Person {
print("s iskiindf Person")
}else{
print("NO")
}
- 类似于oc中isKindOf
二、as、as?、as!(用作类型转换)
import UIKit
class Person{}
let a = 10 as Double
var p:Any = Person()
var p1:Person? = p as? Person
var p2:Person = p as! Person
- as一定可以转换成功
- as?转换可能失败为nil
- as! 必须转换成功,失败了会崩溃。