Swift 1.2 at!

Swift 1.2 (released in Xcode 6.3 Beta 3, redefines the as operator.  The downloadable files are now available in versions for Swift 1.1/Xcode 6.2 and Swift 1.2/Xcode 6.3 and later.

There's a description of the change in the Swift blog on developer.apple.com.

There is now a distinction between guaranteed conversion where the compiler can be certain that the conversion you want to perform will work and forced conversion which may fail. This means that as now has three flavors:

  • as converts from one type to another. The compiler can check if it is valid; if it is not you'll get a compile error. (Put it another way: the developer gets the error.)
  • as! forcabily converts from one type to another. The compiler trusts you, the developer. If the conversion can't occur, you get a runtime exception. (Put it another way: the user gets the error.)
  • as? remains an unwrapping operator for an optional. The compiler checks that you're trying to unwrap an optional, and you can check if the result of unwrapping is nil. (Put it another way: the developer gets the error if it's not an optional, but after that, it's up to the developer to test the unwrapped optional for nil.)

 

Tags: