Lets try! Swift(version: Swift.2)

Swift 2 is here with some new tools to play with. Most of them have been by requests from the community. I haven’t played that much with them yet but here is a brief overview of some of them. Lets start with the simple ones, but yet very welcomed. All examples below have been developed using Xcode 7.0 beta.

Apple has decided to replace the old do-while loop and replace it with repeat-while. Might seem like a small change but its intention is more obvious when you reading code. And do is now used to scope variables.

The new keyword guard is used to avoiding the pyramid of doom when unwrapping optionals. This happend when you were unwrapping layers of optionals. In Swift 1.2 they added the possibily to unwrapped chains of optionals. At first this looks like a regular if let but don’t be fooled. It is an if for non optionals, but unlike if the unwrapped value can be accessed outside the guard statement.

Apple never liked try/catch even if it was supported in Objective-C. They told us to use it while developing our applications, but not in our production code. In Swift 2.0 however they added exception handling, which might look like the one you’ve seen in Java or in C# but Swift’s version is way better. Let me explain why. First of all you don’t have to tell which exceptions your method/function is throwing, just that it throws. You cant even call code in that throws without the “try” -statement.

Another huge advantage in Swift exception handling compared to C# or Java is that Swift execution path for error or success are roughly the same. Your code won’t get any slowdowns using exceptions.

loginUserWithName has a new keyword before its return type, throws. If we want to call this function we must use the keyword try before the function/method name. We can either call it:

try! loginUserWithName(….) 

Or as I do in the gist above. The difference is that the version above we don’t care about the exception message nor exception type. It will turn the exception into a run-time assertion and terminate the application.

You can easily create your own custom exception error types as I done above, just by implementing the ErrorType protocol. Notice that catch actually creates a varible “error” for you which is containing the exception type. It’s also possible to catch different types of ExceptionErrors in the same do/catch statement like the example below.

do {
   try someFuncThatThrows()
} catch ErrorWithMessage(let message){
   print(message)
} catch {
   print(error)
}

Sometimes you want to execute code after an exception is thrown, or just before you leave a current block of code, then you can use defer-keyword. Its often used as a finally block, or when you’re closing a file.

Sometimes you just want the associated value of an list of enums. In Swift 2.0 you dont longer need to write all paths, but you can either use for or if.

There is much much more news in Swift 2, like recursive enums, extended Mirror Api (Reflections), SIMD Vectors, default implementation in protocols, protocol extensions, if/do/break labels and much more.

Follow me on Twitter: https://twitter.com/konrad1977

Swift Programming

The Swift Programming Language

Mikael Konradsson

Written by

Passionate iOS-developer. Backend developer @ Meridium.se

Swift Programming

The Swift Programming Language