iOS-swiftSwift Tipsswift

Swift Tips - as、as!、as?三种类型转换操作符

2018-11-27  本文已影响3人  Ro_bber

这里整理总结下 as、as!、as? 这三种类型转换操作符的异同,以及各自的使用场景。

as使用场合

class Animal {}

class Cat: Animal {}

let cat = Cat()

let animal = cat as Animal

let num1 = 42 as CGFloat
let num2 = 42 as Int
let num3 = 42.5 as Int
let num4 = (42 / 2) as Double
switch animal {
case let cat as Cat:
    print("如果是Cat类型对象,则做相应处理")
case let dog as Dog:
    print("如果是Dog类型对象,则做相应处理")
default: break
}

as!使用场合

向下转型(Downcasting)时使用。由于是强制类型转换,如果转换失败会报 runtime 运行错误。

class Animal {}
class Cat: Animal {}
let animal :Animal  = Cat()
let cat = animal as! Cat

as?使用场合

as? 和 as! 操作符的转换规则完全一样。但 as? 如果转换不成功的时候便会返回一个 nil 对象。成功的话返回可选类型值(optional),需要我们拆包使用。
由于 as? 在转换失败的时候也不会出现错误,所以对于如果能确保100%会成功的转换则可使用 as!,否则使用 as?

let animal:Animal = Cat()
 
if let cat = animal as? Cat{
    print("cat is not nil")
} else {
    print("cat is nil")
}

喜欢我的可以关注收藏我的个人博客:Ro.bber

上一篇 下一篇

猜你喜欢

热点阅读