Swift 1.2 separates the notions of guaranteed conversion and forced conversion into two distinct operators. Guaranteed conversion is still performed with the as
operator, but forced conversion now uses the as!
operator. The !
is meant to indicate that the conversion may fail. This way, you know at a glance which conversions may cause the program to crash.
class Animal {}
class Dog: Animal {}
let a: Animal = Dog()
a as Dog // now raises the error: "'Animal is not convertible to 'Dog';
// ... did you mean to use 'as!' to force downcast?"
a as! Dog // forced downcast is allowed
let d = Dog()
d as Animal // upcast succeeds