1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

27
1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015

description

3 Swift 2.0: What’s new “Guard” Statement 1 Error Handling 2 “Defer” Statement 3 Protocol Extension 4

Transcript of 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

Page 1: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

1

SWIFT 2.0:New featuresDzianis Astravukh

AUGUST 5, 2015

Page 2: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

2

SWIFT 2.0

WWDC 2015

Page 3: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

3

Swift 2.0: What’s new

“Guard” Statement1

Error Handling2

“Defer” Statement3

Protocol Extension4

Page 4: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

4

“GUARD” STATEMENT

Page 5: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

5

“Guard” statement

“A guard statement is used to transfer program control out of a scope if one or more conditions aren’t met.”

guard condition else {statements

}

Page 6: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

6

Hey! I have already “if .. else” construction. Why do I need one more?

Page 7: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

7

“Guard” statement

func fooBinding(x: Int?) {if let x = x where x > 0 {

// Do stuff with xx.description

}

// Value requirements not met, do something}

Page 8: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

8

“Guard” statement

func fooGuard(x: Int?) {guard let x = x where x > 0 else {

 // Value requirements not met, do something

return}

    // Do stuff with xx.description

}

Page 9: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

9

“Guard” statement

Page 10: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

10

“Guard” statement

Page 11: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

11

ERROR HANDLING

Page 12: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

12

Should I really handle all exceptional cases in application?It’s just such pain is the ass…

Page 13: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

13

Error handling in the past

• In Objective-C there main error handling tool was NSError• Exceptions were used to filter “programmer errors” and nobody really cared about catching these

Page 14: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

14

ErrorType in Swift

• In Swift there is a special type (protocol) of enumeration which can be used to represent errors: ErrorType

enum VendingMachineError: ErrorType { case InvalidSelection case InsufficientFunds(required: Double) case OutOfStock }

Page 15: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

15

Throwing errors

• To indicate that a given function or method can fail, it has to be marked with the throws keyword

• Whenever a method/function is called that can throw an ErrorType it has to be used with try in front of it. As an alternative you can use try! to stop error distribution

func vend(itemNamed name: String) throws {guard var item = inventory[name] else {

throw VendingMachineError.InvalidSelection

}…

}

Page 16: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

16

Catching Errors

• To catch errors, you can use the do {...} catch {...} control flow

• Catch can be defined for specific ErrorTypes • If you don’t catch every possible ErrorType, than

every un-handled error type will be thrown forward (travels up on the call stack)

do {try vend(itemNamed: "Candy Bar")

} catch VendingMachineError.InvalidSelection {print("Invalid Selection.")

}

Page 17: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

17

Benefits

• Since Swift has a strict type system (not even nil allowed for non optionals) you don’t have to declare a return value as an optional just because the function can fail

• Error distribution is a lot easier (try, catch, throw)

• You don’t have to use NSError** any more or create a delegate to notify about errors

Page 18: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

18

“DEFER” STATEMENT

Page 19: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

19

“Defer” statement

• Defer blocks can be used to delay the execution of a code block until it’s scope is executed • It is not strictly related to the new error handling flow. It can be used for many reasons. It’s a method for

cleaning up, even if your code failed at some point

// Some scope: {

// Get some resource. defer { // Release resource. } // Do things with the resource. // Possibly return early if an error occurs.

} // Deferred code is executed at the end of the scope.

Page 20: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

20

PROTOCOL EXTENSIONS

Page 21: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

21

Protocol extensions

let ints = [1,2,3,4,5]

//Swift 1map(filter(ints, {$0 % 1 == 0}), {$0 + 1})

//Swift 2ints.map({$0 + 1}).filter({$0 % 1 == 0})

Page 22: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

22

Protocol extensions

extension CollectionType where Generator.Element : Equatable { public func indexOf(element: Generator.Element) -> Index? { for i in self.indices { if self[i] == element { return i } } return nil }}

Page 23: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

23

Protocol extensions

Page 24: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

24

Protocol extensions

protocol ImportantProtocol {func doImportantStuff()

}

//default implementation for functionextension ImportantProtocol {

func doImportantStuff() {print("doing important

stuff")}

}

Page 25: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

25

PROTOCOL-ORIENTED PROGRAMMING

Ha? What does it mean? What about objects and classes?

Page 26: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

26

GOING TO OPEN-SOURCEHow it will influence language growth

Page 27: 1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.

27

THANKS FOR YOUR ATTENTION!