Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

813 views

Published on

Welcome to 2014, the year of the Swift! In this talk, Paul is going to take you through the 10 most interesting Swift topics, and convince you (if that's even needed) why Swift if the language to learn this year. We'll be using Playgrounds and even attempt to create a small app so get your laptops ready and follow along

Published in: Software
  • Be the first to comment

  • Be the first to like this

iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu

  1. 1. 10 reasons you'll love Swift Paul Ardeleanu © 2014 Hello24 Ltd. hello24.com
  2. 2. here
  3. 3. TIOBE Index for Nov 2014 Slide 5 Hello24 Ltd. (c) 2014
  4. 4. TIOBE Index for Nov 2014 Slide 6 Hello24 Ltd. (c) 2014
  5. 5. Why a new language?
  6. 6. Slide 8 Hello24 Ltd. (c) 2014 AAPL
  7. 7. Slide 9 Hello24 Ltd. (c) 2014 Products
  8. 8. Slide 1 0 Hello24 Ltd. (c) 2014 Software
  9. 9. Slide 1 1 Hello24 Ltd. (c) 2014 App Stores
  10. 10. Slide 1 2 Hello24 Ltd. (c) 2014 Apps
  11. 11. Apple needs developers
  12. 12. Slide Hello24 Ltd. (c) 2014 ‣ maximise the number of developers ‣ keeping existing developers happy 14 2 ways…
  13. 13. Slide Hello24 Ltd. (c) 2014 ‣ 30 year old language ‣ drastically different from other languages ‣ not entirely future-proof 15 Objective-C
  14. 14. How did this happen?
  15. 15. LLVM & Clang gcc => llvm-gcc => llvm
  16. 16. Swift Objective-C without C Slide Hello24 Ltd. (c) 2014 19 Objective-C is to Swift == cat is to cattle
  17. 17. 10 reasons you'll love Swift
  18. 18. 1. No C
  19. 19. Slide 2 2 Hello24 Ltd. (c) 2014 No main( ) function
  20. 20. Slide 2 3 Hello24 Ltd. (c) 2014 No [, ] or ;
  21. 21. 2. Modern
  22. 22. Module 02 Slide Hello24 Ltd. (c) 2014 let http404Error = (404, "Not Found") 25 Tuples let x = 1 let y = 2 let point = (x, y) (.0 1, .1 2) point.0 1 let origin = (x: 200, y: 100) origin.y (.0 200, .1 100) 100
  23. 23. Module 02 Slide Hello24 Ltd. (c) 2014 // Original var fibonacci = 1 var prev = 0 while fibonacci < 100 { var prev_tmp = fibonacci fibonacci += prev prev = prev_tmp println(fibonacci) } 26 Fibonacci Tuples // tuples var fibonacci = 1 var prev = 0 while fibonacci < 100 { (prev, fibonacci) = (fibonacci, fibonacci + prev) println(fibonacci) }
  24. 24. Nil coalescing operator var b = a ?? "default value" Module 02 Slide 2 7 Hello24 Ltd. (c) 2014
  25. 25. 3. Safety
  26. 26. Slide Hello24 Ltd. (c) 2014 let theAnswer = 42 29 Constants & variables var numberBooks = 1 var numberBooks: Int = 1
  27. 27. Safety - no implicit conversion Slide 3 0 Hello24 Ltd. (c) 2014 let theAnswer2 = 42 let trueAnswer = Double(theAnswer) + 0.0001 let aNumber:UInt8 = 42 let anotherNumber:Int64 = Int64(aNumber)
  28. 28. Slide 3 1 Hello24 Ltd. (c) 2014 Safety - switch switch (aNumber % 3, aNumber % 5 ) { case (0, 0): println("fizzbuzz") case (0, _): println("fizz") case (_, 0): println("buzz") default: println(aNumber) }
  29. 29. Module 06 Slide 3 2 Hello24 Ltd. (c) 2014 Classes vs. Structures struct Point { var x = 0 var y = 0 static var counter = 0 static let origin = Point() var description: String { get { switch (point.x, point.y) { case (let 0, 0): return "origin" case (let x, 0): return "on the y axis - x = (case (0, let y): return "on the x axis - y = (default: return "(point.x)x(point.y)" } } } func distanceToOrigin() -> Double { return … } } class Fruit { var color: String var diameter: Int var area: Double { get { return self.width * self.height } set(newValue) { self.width = sqrt(newValue) self.height = sqrt(newValue) } } init(color: String, diameter: Int) { self.color = color self.diameter = diameter } func area() -> Double { return self.width * self.height } }
  30. 30. Module 06 Slide 3 3 Hello24 Ltd. (c) 2014 Classes vs. Structures Classes Structs passed by reference value inheritance ✔ ✘ initializers must be defined auto-generated member-wise initialiser deinitializer ✔ ✘ introspection ✔ ✘ ideal for complex data relatively simple data *
  31. 31. Module 05 Slide 3 4 Hello24 Ltd. (c) 2014 Value type (Structures) var point1 = Point(x: 100, y: 200) var point2 = point1 x: 100 point1.x = 120 point1 y : 200 x: 100 point2 y : 200
  32. 32. Module 05 Slide 3 5 Hello24 Ltd. (c) 2014 Value type (Structures) var point1 = Point(x: 100, y: 200) var point2 = point1 x: 120 point1.x = 120 point1 y : 200 x: 100 point2 y : 200
  33. 33. Module 06 Slide 3 6 Hello24 Ltd. (c) 2014 Reference type (Classes) var rectangle1 = Rect() rectangle1.width = 100 var rectangle2 = rectangle1 rectangle2.width = 200 width: 100 rectangle1 height: 0 rectangle2
  34. 34. Module 06 Slide 3 7 Hello24 Ltd. (c) 2014 Reference type (Classes) var rectangle1 = Rect() rectangle1.width = 100 var rectangle2 = rectangle1 rectangle2.width = 200 width: 200 rectangle1 height: 0 rectangle2
  35. 35. nil
  36. 36. 4. Optionals
  37. 37. ❗️ Module 02 Slide 4 1 Hello24 Ltd. (c) 2014 Optionals var answer: Int = 42 var theAnswer: Int = nil var theAnswer: Int? ✔ theAnswer = 42 nil {Some 42}
  38. 38. Optionals == Schrödinger’s cat Can either: ‣ be nil ‣ contain a value Module 02 Slide 4 2 Hello24 Ltd. (c) 2014
  39. 39. Module 02 Slide 4 3 Hello24 Ltd. (c) 2014 Using an optional var theAnswer: Int? if theAnswer != nil { var x = 4 + theAnswer } ❗️
  40. 40. Module 02 Slide 4 4 Hello24 Ltd. (c) 2014 Forced Unwrapping var theAnswer: Int? if theAnswer != nil { var x = 4 + theAnswer! } ✔
  41. 41. Module 02 Slide 4 5 Hello24 Ltd. (c) 2014 Optional binding if theAnswer != nil { if let answer = theAnswer { var x = 4 + answer } var x = 4 + theAnswer! } no !
  42. 42. Module 02 Slide 4 6 Hello24 Ltd. (c) 2014 Optionals var theAnswer: Int? if theAnswer != nil { var x = 4 + theAnswer! } theAnswer!
  43. 43. Module 02 Slide 4 7 Hello24 Ltd. (c) 2014 Optionals var theAnswer = 42 var theAnswer: Int? var theAnswer: Int! - always has a value - type can be inferred - either nil or has a value - must be unwrapped - nil until first assignment - assumed to always have a value
  44. 44. Nil coalescing operator Module 02 Slide 4 8 Hello24 Ltd. (c) 2014 var theAnswer: Int? var x = theAnswer ?? 12 equivalent to: var x = (theAnswer != nil) ? theAnswer! : 12
  45. 45. 5. Mix & match
  46. 46. Slide 5 0 Hello24 Ltd. (c) 2014 Swift project
  47. 47. Slide 5 1 Hello24 Ltd. (c) 2014 Bridging
  48. 48. Slide 5 2 Hello24 Ltd. (c) 2014 Bridging
  49. 49. Slide 5 3 Hello24 Ltd. (c) 2014 Bridging class STDataObject: NSManagedObject { @NSManaged var uuid: String? @NSManaged var sync_uuid: String? @NSManaged var is_active: NSNumber class func managedObjectContext() -> NSManagedObjectContext? { var appDelegate = UIApplication.sharedApplication().delegate as AppDelegate return appDelegate.managedObjectContext }
  50. 50. 6. Functions as first class citizens
  51. 51. Module 04 Slide 5 5 Hello24 Ltd. (c) 2014 Function structure function name parameter name parameter type func greet(title: String, person: String) -> String { return "Hello (title) (person)" } return type greet("Mr.", "Paul") “Hello Mr. Paul"
  52. 52. Module 04 Slide 5 6 Hello24 Ltd. (c) 2014 Nesting func greet(var person: String) -> String { func morningGreetings(person: String) -> String { return "Good morning (person)" } func afternoonGreetings(person: String) -> String { return "Good afternoon (person)" } let morning = true var greeting: (String) -> String if morning { greeting = morningGreetings } else { greeting = afternoonGreetings } return greeting(person) } greet("Paul")
  53. 53. Function as return type func greetingAt(hour: Int) -> (String) -> String { func morningGreeting(name: String) -> String { Module 04 Slide 5 7 Hello24 Ltd. (c) 2014 return "Good morning (name)" } func afternoonGreeting(name: String) -> String { return "Good afternoon (name)" } return hour < 12 ? morningGreeting : afternoonGreeting } greetingAt(11)("Paul") greetingAt(14)("Paul") “Good morning Paul" “Good afternoon Paul"
  54. 54. Functions are Closures
  55. 55. Module 04 Slide 5 9 Hello24 Ltd. (c) 2014 Blocks in Objective-C NSString *(^sayHello)(NSString *); sayHello = ^(NSString *name) { return [NSString stringWithFormat:@"Hello %@", name]; }; Person *theUser = [[Person alloc] initWithName:@"Paul"]; [theUser welcomeUserWithBlock:^(NSString *name) { NSLog(@"Hello %@", name); }];
  56. 56. Module 04 Slide 6 0 Hello24 Ltd. (c) 2014 Blocks in Objective-C func greetWithMessage(name: String, message: (String) -> String) -> String { return message(name); } greetWithMessage("Paul", { (name: String) -> String in return "Good morning (name)" } )
  57. 57. Module 04 Slide Hello24 Ltd. (c) 2014 • global function • have name & don’t capture environment • nested function • have name & capture environment • closure expressions • no name & can capture environment 61 Types of Closures
  58. 58. 7. Magic
  59. 59. Closures - Short Syntax func greetWithMessage(name: String, message: (String) -> String) -> String { Module 04 Slide 6 3 Hello24 Ltd. (c) 2014 return message(name); } greetWithMessage("Paul", { (name: String) -> String in return "Good morning (name)" } ) greetWithMessage("Paul", { name in return "Good morning (name)" }) greetWithMessage("Paul", { return "Good morning ($0)" })
  60. 60. Module 04 Slide 6 4 Hello24 Ltd. (c) 2014 Trailing closures func greetWithMessage(name: String, message: (String) -> String) -> String { return message(name); } greetWithMessage("Paul", { name in return "Good morning (name)" }) greetWithMessage("Paul") { name in return "Good morning (name)" } greetWithMessage("Paul") { return "Good morning ($0)" }
  61. 61. 8. Unicode
  62. 62. 8. Emoji
  63. 63. Module 02 Slide 6 7 Hello24 Ltd. (c) 2014 Characters & Strings let dog = "" let fizz = "" let buzz = "" var fizzbuzz = fizz + buzz ""
  64. 64. Module 02 Slide 6 8 Hello24 Ltd. (c) 2014 Character Viewer
  65. 65. Module 02 Slide Hello24 Ltd. (c) 2014 let  = "dog" let  = "cat" let  = "ladybug" let ❄️ = "⛄️" 69 Emoji
  66. 66. Module 02 Slide 7 0 Hello24 Ltd. (c) 2014
  67. 67. Slide 7 1 Hello24 Ltd. (c) 2014
  68. 68. "ş" Module 02 Slide 7 2 Hello24 Ltd. (c) 2014 Unicode let a = "u{103}" let sh = "u{15F}" "ă" "ş" Extended grapheme cluster ș = s + cedilla let cedilla = "u{327}" "̧" let sh2 = "u{73}u{327}" "u{15F}" == "u{73}u{327}" true
  69. 69. Module 02 Slide 7 3 Hello24 Ltd. (c) 2014 Unicode let sh = "u{15F}" "ş" let realSh = "u{219}" "ș"
  70. 70. 9. Playgrounds
  71. 71. Slide 7 6 Hello24 Ltd. (c) 2014 New Playground
  72. 72. Slide 7 7 Hello24 Ltd. (c) 2014 Empty Playground
  73. 73. Slide 7 8 Hello24 Ltd. (c) 2014 Playground
  74. 74. Slide 7 9 Hello24 Ltd. (c) 2014 Playground
  75. 75. Slide 8 0 Hello24 Ltd. (c) 2014 Fibonacci
  76. 76. Slide 8 1 Hello24 Ltd. (c) 2014 Fibonacci
  77. 77. Slide 8 2 Hello24 Ltd. (c) 2014 Fibonacci
  78. 78. Slide 8 3 Hello24 Ltd. (c) 2014 Playground - Timeline
  79. 79. Slide Hello24 Ltd. (c) 2014 ‣ Interactive experience ‣ Immediate feedback ‣ Watch code progression through loops ‣ Easy way to ‣ prototype ‣ test snippets of code ‣ CAREFUL! Executed automatically. 84 Playgrounds
  80. 80. 10. Swift REPL
  81. 81. Slide 8 6 Hello24 Ltd. (c) 2014 REPL Read–eval–print loop $ which swift /usr/bin/swift $ swift -version Swift version 1.1 (swift-600.0.54.20) Target: x86_64-apple-darwin14.0.0
  82. 82. Slide 8 7 Hello24 Ltd. (c) 2014 man swift
  83. 83. Slide 8 8 Hello24 Ltd. (c) 2014 swift -- help
  84. 84. $ swift Welcome to Swift! Type :help for assistance. 1> 1 + 2 $R0: Int = 3 2> "once upon a time" $R1: String = "once upon a time" 3> $R1 + " there were ($R0) bears" $R2: String = "once upon a time there were 3 bears" 4> println($R2) once upon a time there were 3 bears Slide Hello24 Ltd. (c) 2014 89 REPL
  85. 85. ‣ iOS 8 released yesterday ‣ Swift is v.1.0 as of Sept 9th ‣ Xcode 6.0.1 released yesterday ‣ Apps written in Swift started being accepted on Sept 9th Slide Hello24 Ltd. (c) 2014 90 Resources
  86. 86. ‣ iOS 8 released yesterday ‣ Swift is v.1.0 as of Sept 9th ‣ Xcode 6.0.1 released yesterday ‣ Apps written in Swift started being accepted on Sept 9th Slide Hello24 Ltd. (c) 2014 91 Resources
  87. 87. 10 reasons you’ll love Swift 6.Functions - 1st class citizens 7.Magic 8.Unicode 9.Playgrounds 10.REPL Slide Hello24 Ltd. (c) 2014 1.No C 2.Modern 3.Safety 4.Optionals 5.Mix & match 92
  88. 88. Objective-C ➾ Swift
  89. 89. Slide 9 4 Hello24 Ltd. (c) 2014
  90. 90. One more thing…
  91. 91. WatchKit
  92. 92. Thank you! Paul Ardeleanu pa@h24.io @pardel © 2014 Hello24 Ltd. hello24.com

×