as! vs as operator in Xcode 6.3 in Swift

前端 未结 3 1786
长情又很酷
长情又很酷 2021-01-29 08:23

Swift changed a lot with Xcode 6.3. I had to replace dozens of places in each of my app as -> as!. Why, what are the rules now?

3条回答
  •  无人共我
    2021-01-29 08:54

    According to the release notes:

    The notions of guaranteed conversion and “forced failable” conversion are now separated into two operators. Forced failable conversion now uses the as! operator. The ! makes it clear to readers of code that the cast may fail and produce a runtime error. The “as” operator remains for upcasts (e.g. “someDerivedValue as Base”) and type annotations (“0 as Int8”) which are guaranteed to never fail.

提交回复
热议问题