在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
忽略的基础性的问题,应该早点写出这个问题的解释比较好,毕竟 swift 好多的地方都用到了 as as!as?,是非常容易搞晕大脑的 1,as使用场合 class Animal {} class Cat: Animal {} let cat = Cat() let animal = cat as Animal (2)消除二义性,数值类型转换 let num1 = 42 as CGFloat let num2 = 42 as Int let num3 = 42.5 as Int let num4 = (42 / 2) as Doubl (3)switch 语句中进行模式匹配 switch animal { case let cat as Cat: print("如果是Cat类型对象,则做相应处理") case let dog as Dog: print("如果是Dog类型对象,则做相应处理") default: break } class Animal {} class Cat: Animal {} let animal :Animal = Cat() let cat = animal as! Cat let animal:Animal = Cat() if let cat = animal as? Cat{ print("cat is not nil") } else { print("cat is nil") }
|
请发表评论