Swift 2.0 In A Nutshell

Back in July, 2014 Apple announced its new programming language, Swift. It’s 1 year now since the first announcement, and it seems that the Swift team was working hard preparing the next big release; Swift 2.0. In this post I’m going to highlight the most amazing features in the new release, and as I promised, in a nutshell.Apple-Swift-2.0

Error Handling

Believe it or not, try-catch is now available in Swift 2.0. It’s a total paradigm shift in the way Swift & Objective-C developers used to handle errors. Before 2.0, developers used to pass NSError object (inout parameter) to functions that may encounter an exceptional condition which prevents the function from completing the assigned task, and then check this object for nullability to see whether any error took place or not. But unfortunately you could pass nil instead of a reference, or pass a reference but never check it for error.


In Swift 2.0, Apple introduced its unique version from try-catch, yes you read it right, its unique version! Instead of the traditional try-catch we see in Java or C#. Apple introduced the do-try-catch block. See the below code for better illustration.

The old way of handling errors:

if doSomethingWithError(nil) {
  print("Could not do anything! :(")

In Swift 2.0

// Define your error by extending ErrorType
enum DoError: ErrorType {
 case VeryBadError
// Declare that your function may throw an error
func doSomething() throws {
 if do.everythingIsFine() {
 // Let’s do it!
 } else {
 // Throw if something bad happened
 throw DoError.VeryBadError
func tryToDoIt() {
 // Check for errors and handle if happened
 do {
 try doSomething()
 } catch {
 print("Could not do it! :(")

Protocol Extensions

In Swift 1.0 you could only extend only classes, enums and structs. Protocols were like interfaces, you can’t extend them. In 2.0 you Swift team brought the power of extensions to protocols, you now can extend protocols by adding new functions and properties.

More about protocol extensions in Swift 2.0 on RayWenderlich.


Code for the happy scenario!

In Swift 1.2, the Swift team -thankfully- helped us a lot by getting rid of the pyramid of doom. But you were still have to wrap your happy scenario code in an unneeded indented scope. Guard in 2.0 enables you to check your optionals and unwrap them at the beginning of the function, and continue working with the unwrapped version anywhere in the function. You can think of guard more like an Assert, but rather than crashing, you can gracefully exit.

func fooGuard(x: Int?) {
 guard let x = x where x > 0 else {
 // Value requirements not met, do something
 // Do stuff with x, it’s automatically unwrapped

More on why guard is better than if on Natasha’s blog also in Eric Cerny’s blog.

Other News

Swift will be open source

Yes, Swift 2.0 will be open source. Are we expecting other non-iOS platforms to use Swift? Maybe.

No more println!!

In Swift 2.0 you won’t be able to use your go-to logging function println(). But you’ll use print() which takes a default second parameter to concatenate new line character.


Swift 2 introduces the defer keyword, which effectively means “here’s some what I want you to do later, no matter what.” More on the topic.

Where to go from here


6 thoughts on “Swift 2.0 In A Nutshell

  1. Pingback: try, try! & try? what’s the difference, and when to use each? | Ahmed Abdurrahman

  2. Pingback: попробуйте, попробуйте! & пытаться? В чем разница, и когда использовать каждый? — Вопросы и ответы по программированию

  3. Pingback: Music is Everything. Songs to the TikTok Videos | free-instagram.com

  4. Pingback: try, try! & try? what’s the difference, and when to use each? - PhotoLens

  5. Pingback: [Swift] try, try! & try? what’s the difference, and when to use each? - Pixorix

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s