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

Taking Swift 2 for a spin

Jan Riehn
October 02, 2015

Taking Swift 2 for a spin

One year after Swift has been released, Apple introduced with Swift 2.0 the programming language of the future. Rather than just running only in an Apple environment, it will also run on an Unix environment and will be open sourced later this year. This years Github survey already placed Swift on the first place of programmers favourite languages. This talk will take the attendees (beginner to expert) for a spin through the new language features, as well as Xcode 7.0 IDE features. We will give a lot of hands on tipps and tricks, from our experience (> 1 year) with Swift in a production environment.

Jan Riehn

October 02, 2015
Tweet

More Decks by Jan Riehn

Other Decks in Programming

Transcript

  1. Bindings  -­‐  ObjC  Style func bindingObjC(x: Int?) { if x

    == nil || x <= 0 { return } x!.description }
  2. func bindingObjC(x: Int?) { // Checking for a condition that

    you don’t want if x == nil || x <= 0 { return } x!.description } Bindings  -­‐  ObjC  Style
  3. func bindingObjC(x: Int?) { if x == nil || x

    <= 0 { // Value requirements not met, do something return } x!.description } Bindings  -­‐  ObjC  Style
  4. func bindingObjC(x: Int?) { if x == nil || x

    <= 0 { return } // Force unwrap the optional value x!.description } Bindings  -­‐  ObjC  Style
  5. func bindingsWithIfLet(x: Int?) { if let x = x where

    x > 0 { x.description } } Bindings  -­‐  Swi*  1.2
  6. func bindingsWithIfLet(x: Int?) { if let x = x where

    x > 0 { // Putting your desired code within all the conditions x.description } } Bindings  -­‐  Swi*  1.2
  7. func getJsonResponse(response: NSObject?) { if let response = response {

    if let dict = response as? [String: NSObject] { if let user = dict["user"] as? [String : NSObject] { if let address = user["address"] as? [String: NSObject] { if let country = address["country"] as? String { print(country) } } } } } } Bindings  -­‐  Pyramid  of  Doom
  8. func bindingsWithIfLet(x: Int?) { if let x = x where

    x > 0 { x.description } // Value requirements not met, do something } Bindings  -­‐  Swi*  1.2
  9. func bindingsWithGuard(x: Int?) { guard let x = x where

    x > 0 else { return } x.description } Bindings  -­‐  Swi*  2.0
  10. func bindingsWithGuard(x: Int?) { // Checking for the condition you

    do want guard let x = x where x > 0 else { return } x.description } Bindings  -­‐  Swi*  2.0
  11. func bindingsWithGuard(x: Int?) { guard let x = x where

    x > 0 else { // Exit early return } x.description } Bindings  -­‐  Swi*  2.0
  12. func bindingsWithGuard(x: Int?) { guard let x = x where

    x > 0 else { return } // Optional variables are autom. unwrapped x.description } Bindings  -­‐  Swi*  2.0
  13. func printFile(fileName: String) { let file = File.openFile(fileName) while let

    fileLine = file.readline() { print(fileLine, appendNewline: true) } closeFile(file) } Defer  -­‐  Swi*  2.0
  14. func printFile(fileName: String) { let file = File.openFile(fileName) defer {

    closeFile(file) } while let fileLine = file.readline() { print(fileLine, appendNewline: true) } } Defer  -­‐  Swi*  2.0
  15. func printFile(fileName: String) { let file = File.openFile(fileName) //defer declaration

    defer { closeFile(file) } while let fileLine = file.readline() { print(fileLine, appendNewline: true) } //defer execution shortly before scope ends } Defer  -­‐  Swi*  2.0
  16. func deferAll() { defer { print("A") } defer { print("B")

    } defer { print("C") } } // C, B, A deferAll() Defer  -­‐  Swi*  2.0  -­‐  LIFO
  17. func shuffleData() { defer { close(inputFile) } defer { close(outputFile)

    break // or return, or throwing an error } // do something } Defer  -­‐  Swi*  2.0  -­‐  DON’Ts
  18. public enum WeekendError: ErrorType { case WorkAllWeekend case Overtime(hoursWorked:Int) }

    Error  Handling  -­‐  Swi*  2.0  -­‐  ErrorType
  19. func haveAWeekend(extraWork:Int) throws { guard (extraWork == 0) else {

    throw WeekendError.Overtime(hoursWorked: extraWork) } print("Let's have a weekend!!") } try haveAWeekend(1) Error  Handling  -­‐  Swi*  2.0  -­‐     Error  Propaga>on
  20. // throws indicates something can go wrong here func haveAWeekend(extraWork:Int)

    throws { guard (extraWork == 0) else { throw WeekendError.Overtime(hoursWorked: extraWork) } print("Let's have a weekend!!") } try haveAWeekend(1) Error  Handling  -­‐  Swi*  2.0  -­‐     Error  Propaga>on
  21. func haveAWeekend(extraWork:Int) throws { guard (extraWork == 0) else {

    // throwing the exception throw WeekendError.Overtime(hoursWorked: extraWork) } print("Let's have a weekend!!") } try haveAWeekend(1) Error  Handling  -­‐  Swi*  2.0  -­‐     Error  Propaga>on
  22. func haveAWeekend(extraWork:Int) throws { guard (extraWork == 0) else {

    throw WeekendError.Overtime(hoursWorked: extraWork) } print("Let's have a weekend!!") } // presence of throws requires a try try haveAWeekend(1) Error  Handling  -­‐  Swi*  2.0  -­‐     Error  Propaga>on
  23. do { try haveAWeekend(4) } catch { print("Gulping the weekend

    exception") } Error  Handling  -­‐  Swi*  2.0  -­‐     Catching  Errors
  24. do { try haveAWeekend(4) } catch WeekendError.WorkAllWeekend { print("You worked

    48 hours :-0") } Error  Handling  -­‐  Swi*  2.0  -­‐     Catching  Errors
  25. do { try haveAWeekend(4) } catch WeekendError.Overtime(let hoursWorked) { print("You

    worked \(hoursWorked) more than you should") } Error  Handling  -­‐  Swi*  2.0  -­‐     Catching  Errors
  26. extension String { var textualDescription: String { return "this is

    an extension \(self) " } } Protocol  Extensions  -­‐  Swi*  1.0
  27. protocol TextRepresentable { var textualDescription: String { get } }

    Protocol  Extensions  -­‐  Swi*  1.0
  28. extension Dice: TextRepresentable { var textualDescription: String { return "A

    \(sides)-sided dice" } } Protocol  Extensions  -­‐  Swi*  2.0
  29. extension Friend: TextRepresentable { var textualDescription: String { return "Share

    a Coke with \(name) " } } Protocol  Extensions  -­‐  Swi*  2.0
  30. Protocol  Extensions  -­‐  Swi*  2.0 Object oriented Protocol oriented super

    class class Data Data Behaviour Behaviour struct Data protocol Behaviour Behaviour
  31. protocol Creatable { var uid: NSUUID { get } var

    data: [String:String] { get } } Let’s  build  a  persistency  framework!
  32. extension Creatable { func save() throws { AwesomeFrameWork.save(uid: uid, data:

    data) } } Let’s  build  a  persistency  framework!
  33. protocol Updateable { var uid: NSUUID { get } var

    data: [String:String] { get } } protocol Deleteable { var uid: NSUUID { get } } Let’s  build  a  persistency  framework!
  34. struct AccountEntity: Creatable,Updateable,Deleteable { let uid: NSUUID let data: [String:String]

    } try AccountEntity(uid: NSUUID(), data: ["name" : "Jan"]).save() Let’s  build  a  persistency  framework!
  35. @testable import MyApp func testMyApp() { // access internal properties,

    methods, and classes } @testable  -­‐  Swi*  2.0
  36. if ((NSClassFromString("WKWebView")) != nil) { print("WKWebView is available") } else

    { print("WKWebView is notavailable") } Availability  Checking  -­‐  Swi*  1.0
  37. @available(iOS 8.0, *) func newFancyFeature() { … } if #available(iOS

    8.0, OSX 10.10, watchOS 2, *) { newFancyFeature() } Availability  Checking  -­‐  Swi*  2.0
  38. • Control  flow  with  repeat   • Migrator  to  Swift

     2.0   • … And  there  is  s>ll  more