Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015."— Presentation transcript:

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

2 2 SWIFT 2.0 WWDC 2015

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

4 4 “GUARD” STATEMENT

5 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 }

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

7 7 “Guard” statement func fooBinding(x: Int?) { if let x = x where x > 0 { // Do stuff with x x.description } // Value requirements not met, do something }

8 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 x x.description }

9 9 “Guard” statement

10 10 “Guard” statement

11 11 ERROR HANDLING

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

13 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

14 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 }

15 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 } … }

16 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.") }

17 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

18 18 “DEFER” STATEMENT

19 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.

20 20 PROTOCOL EXTENSIONS

21 21 Protocol extensions let ints = [1,2,3,4,5] //Swift 1 map(filter(ints, {$0 % 1 == 0}), {$0 + 1}) //Swift 2 ints.map({$0 + 1}).filter({$0 % 1 == 0})

22 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 }

23 23 Protocol extensions

24 24 Protocol extensions protocol ImportantProtocol { func doImportantStuff() } //default implementation for function extension ImportantProtocol { func doImportantStuff() { print("doing important stuff") }

25 25 PROTOCOL-ORIENTED PROGRAMMING Ha? What does it mean? What about objects and classes?

26 26 GOING TO OPEN-SOURCE How it will influence language growth

27 27 THANKS FOR YOUR ATTENTION!


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

Similar presentations


Ads by Google