Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What's New in Swift 2.0

What's New in Swift 2.0

Parveen Kaler

July 07, 2015
Tweet

More Decks by Parveen Kaler

Other Decks in Programming

Transcript

  1. What's New in Swift 2.0 © Smartful Studios, 2015 •

    http://parveenkaler.com • @kaler
  2. What's New in Swift 2.0 1. Protocols 2. Error handling

    3. Control flow © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
  3. What's New in Swift 2.0 1. Protocols 2. Error handling

    3. Control flow © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
  4. What is a Protocol? The Basics protocol SomeType { var

    someVar: Int { get set } static var someTypeVar: Int func someFunc() static func someTypeFunc() } © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
  5. What is a Protocol? Even More • mutating keyword •

    init, required init • @objc and optional © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
  6. What's New with Protocols Protocol Extensions allow protocols to contain

    method implementations, not just declarations. © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
  7. Protocol Extenstion with Type Constraint extension CollectionType where Self.Generator.Element: IntegerArithmeticType

    { var average: Self.Generator.Element { var accumulate = 0 var count = 0 for c in self { accumulate += c count++ } return count == 0 ? 0 : accumulate/count } } © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
  8. Extensions & Dynamic Dispatch protocol P { func this() }

    extension P { func this() { print("This extension") } func that() { print("That extension") } } struct S: P { func this() { print("This struct") } func that() { print("That struct") } } let s = S() s.this() // "This struct" s.that() // "That extension" © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
  9. What's New in Swift 2.0 1. Protocols 2. Error handling

    3. Control flow © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
  10. Swift 2.0 Error Model • Looks like exceptions but it's

    actually return values • Forces checking of return value © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
  11. Old Cocoa Error Handling NSError *error; BOOL success = [object

    functionMayFailWithError:&error]; if (success == NO) { // Check the error variable } © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
  12. Swift 2.0 Error Handling do { try object.firstFunctionThatMayFail() // the

    try is mandatory try object.secondFunctionThatMayFail() } catch { } © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
  13. Representing Errors Conform to the empty ErrorType protocol. enum HTTPError

    : ErrorType { case BadRequest = 400 case Unauthorized = 401 case Forbidden = 403 case NotFound = 404 } © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
  14. "Throwing" Errors struct WeirdError : ErrorType { let error: String

    } func functionThatMayThrow() throws -> Int { if someStateIsWeird { throw WeirdError } return 0 } © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
  15. "Catching" Errors do { try functionThatMayThrow() } catch WeirdError {

    print("Weird Error") } © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
  16. "Catching" Errors • catch patterns are like switch • must

    be exhaustive or function must be marked as throws to propogate • an empty pattern catches all errors © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
  17. Force Try try! functionThatMayThrow() • disables error propagation • Warning:

    runtime assertion if an exception is thrown © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
  18. What's New in Swift 2.0 1. Protocols 2. Error handling

    3. Control flow © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
  19. defer func readFile(filename: String) throws { let file = open(filename)

    defer { close(file) } while let line = readline(file) { ... } // defer is executed here } © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
  20. defer • if there are multiple defer blocks in a

    scope, they are executed in reverse order • defer blocks CAN NOT return or break © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
  21. guard func printName(name: String?) { guard let name = name

    else { return } print("Hello \(name)") } © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
  22. guard • optional bindings are available for the rest of

    the code block • guard MUST call return, break, or continue © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
  23. guard & defer Like chocolate and peanut butter func readFile(filename:

    String) { guard let file = open(filename) else { return } defer { close(file) } while let line = readline(file) { ... } } © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
  24. What's New in Swift 2.0 • @kaler - http://parveenkaler.com •

    @smartful - http://smartfulstudios.com • @SwiftNewsCo - http://swiftnews.co © Smartful Studios, 2015 • http://parveenkaler.com • @kaler