What’s new in Swift 2.0

Home / Uncategorized / What’s new in Swift 2.0

Coming with Xcode 7, introduced at WWDC 2015 in June, swift will be updated to 2.0 and there are a lot of useful changes:

Improved error handling

you can now add throws keyword to your method signature. So you can define an enum ErrorType and throw this specific error in that method. when using the method, you need to surround it with a try, catch block then.
For Example:

enum DriveError : ErrorType {

case .NoEngine
case .BrokenEngine

func drive(car Car, route Route) throws DriveError {

if car.hasEngine() {
} else {
throw DriveError.NoEngine

// usage of the method using try catch:
do {

let car = Car()
let route = Route(start:”San Francisco”, destination:”New York”)
try drive(car, route)
} catch {
// Try another car / add engine to the car ?



Remember, in Swift 1.2 Apple added the possibility to bind more then one optionals in single line, like:

if let book = book, let text = text { ... }

in Swift 2.0 they introduce the guard expression, with guard it’s much easier to exit earlier, if some variables are not available / nil

guard let book = book, let text = text else {

// At this point, everything is unwrapped

Protocol extensions

This is actually a really nice new feature. Until now, you were able to create protocols which you could adapt to your classes. What you weren’t able to do, was to add standard implementations for the predefined methods. With protocol extensions now you can:

protocol drivable {
func move(distance Float)
func turn(direction Direction)

extension drivable {
func head50MetersToEast() {

Other small changes

print line println("Hello World") is now just print("Hello World")

improvements in arrays: you can now just check if objects are inside by using array.contains(object) e.g.

let persons = ["alex", "stefan", "michael"]

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.