iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu
-
Upload
hello24com -
Category
Software
-
view
252 -
download
1
Transcript of iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu
© 2014 Hello24 Ltd. hello24.com
10 reasons you'll love SwiftPaul Ardeleanu
here
Slide Hello24 Ltd. (c) 20145
TIOBE Index for Nov 2014
Slide Hello24 Ltd. (c) 20146
TIOBE Index for Nov 2014
Why a new language?
Slide Hello24 Ltd. (c) 20148
AAPL
Slide Hello24 Ltd. (c) 20149
Products
Slide Hello24 Ltd. (c) 201410
Software
Slide Hello24 Ltd. (c) 201411
App Stores
Slide Hello24 Ltd. (c) 201412
Apps
Apple needs developers
Slide Hello24 Ltd. (c) 2014
‣ maximise the number of developers
‣ keeping existing developers happy
14
2 ways…
Slide Hello24 Ltd. (c) 2014
‣ 30 year old language
‣ drastically different from other languages
‣ not entirely future-proof
15
Objective-C
How did this happen?
LLVM & Clang
gcc => llvm-gcc => llvm
Slide Hello24 Ltd. (c) 2014
Objective-C without C
19
Objective-C is to Swift == cat is to cattle
Swift
10 reasons you'll love Swift
1. No C
Slide Hello24 Ltd. (c) 201422
No main( ) function
Slide Hello24 Ltd. (c) 201423
No [, ] or ;
2. Modern
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
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) }
Module 02 Slide Hello24 Ltd. (c) 201427
Nil coalescing operator
var b = a ?? "default value"
3. Safety
Slide Hello24 Ltd. (c) 2014
let theAnswer = 42
29
Constants & variables
var numberBooks = 1
var numberBooks: Int = 1
Slide Hello24 Ltd. (c) 201430
Safety - no implicit conversion
let theAnswer2 = 42
let trueAnswer = Double(theAnswer) + 0.0001
let aNumber:UInt8 = 42
let anotherNumber:Int64 = Int64(aNumber)
Slide Hello24 Ltd. (c) 201431
Safety - switch
switch (aNumber % 3, aNumber % 5 ) { case (0, 0): println("fizzbuzz") case (0, _): println("fizz") case (_, 0): println("buzz") default: println(aNumber) }
Module 06 Slide Hello24 Ltd. (c) 201432
Classes vs. Structuresstruct 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 = \(x)" case (0, let y): return "on the x axis - y = \(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 } }
Module 06 Slide Hello24 Ltd. (c) 201433
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 *
Module 05 Slide Hello24 Ltd. (c) 201434
Value type (Structures)
var point1 = Point(x: 100, y: 200) var point2 = point1
x: 100 y : 200point1
x: 100 y : 200point2
point1.x = 120
Module 05 Slide Hello24 Ltd. (c) 201435
Value type (Structures)
var point1 = Point(x: 100, y: 200) var point2 = point1
x: 120 y : 200point1
x: 100 y : 200point2
point1.x = 120
Module 06 Slide Hello24 Ltd. (c) 201436
Reference type (Classes)
var rectangle1 = Rect() rectangle1.width = 100
width: 100 height: 0rectangle1
rectangle2
var rectangle2 = rectangle1
rectangle2.width = 200
Module 06 Slide Hello24 Ltd. (c) 201437
Reference type (Classes)
var rectangle1 = Rect() rectangle1.width = 100
width: 200 height: 0rectangle1
rectangle2
var rectangle2 = rectangle1
rectangle2.width = 200
nil
4. Optionals
Module 02 Slide Hello24 Ltd. (c) 201441
Optionals
var answer: Int = 42
var theAnswer: Int = nil
var theAnswer: Int? ✔
theAnswer = 42
nil
{Some 42}
❗️
Module 02 Slide Hello24 Ltd. (c) 201442
Optionals == Schrödinger’s cat
Can either:
‣ be nil
‣ contain a value
Module 02 Slide Hello24 Ltd. (c) 201443
Using an optional
var theAnswer: Int?
if theAnswer != nil { var x = 4 + theAnswer }
❗️
Module 02 Slide Hello24 Ltd. (c) 201444
Forced Unwrapping
var theAnswer: Int?
if theAnswer != nil { var x = 4 + theAnswer! }
✔
Module 02 Slide Hello24 Ltd. (c) 201445
Optional binding
if let answer = theAnswer { var x = 4 + answer }
if theAnswer != nil { var x = 4 + theAnswer! }
no !
Module 02 Slide Hello24 Ltd. (c) 201446
Optionals
var theAnswer: Int?
if theAnswer != nil { var x = 4 + theAnswer! }
theAnswer!
Module 02 Slide Hello24 Ltd. (c) 201447
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
Module 02 Slide Hello24 Ltd. (c) 201448
Nil coalescing operator
equivalent to:
var theAnswer: Int?
var x = theAnswer ?? 12
var x = (theAnswer != nil) ? theAnswer! : 12
5. Mix & match
Slide Hello24 Ltd. (c) 201450
Swift project
Slide Hello24 Ltd. (c) 201451
Bridging
Slide Hello24 Ltd. (c) 201452
Bridging
Slide Hello24 Ltd. (c) 201453
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 }
6. Functions as first class citizens
Module 04 Slide Hello24 Ltd. (c) 201455
Function structure
func greet(title: String, person: String) -> String { return "Hello \(title) \(person)" }
function name
return type
greet("Mr.", "Paul") “Hello Mr. Paul"
parameter name
parameter type
Module 04 Slide Hello24 Ltd. (c) 201456
Nestingfunc 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")
Module 04 Slide Hello24 Ltd. (c) 201457
Function as return typefunc greetingAt(hour: Int) -> (String) -> String { func morningGreeting(name: String) -> String { 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"
Functions are Closures
Module 04 Slide Hello24 Ltd. (c) 201459
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); }];
Module 04 Slide Hello24 Ltd. (c) 201460
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)" } )
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
7. Magic
Module 04 Slide Hello24 Ltd. (c) 201463
Closures - Short Syntaxfunc greetWithMessage(name: String, message: (String) -> String) -> String { 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)" })
Module 04 Slide Hello24 Ltd. (c) 201464
Trailing closuresfunc 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)" }
8. Unicode
8. Emoji
Module 02 Slide Hello24 Ltd. (c) 201467
Characters & Strings
let fizz = "🍹" let buzz = "🎉"
var fizzbuzz = fizz + buzz "🍹🎉"
let dog = "🐶"
Module 02 Slide Hello24 Ltd. (c) 201468
Character Viewer
Module 02 Slide Hello24 Ltd. (c) 2014
let 🐶 = "dog"
let 🐱 = "cat"
let 💃🐞 = "ladybug"
let ❄️🚶 = "⛄️"
69
Emoji
Module 02 Slide Hello24 Ltd. (c) 201470
Slide Hello24 Ltd. (c) 201471
Module 02 Slide Hello24 Ltd. (c) 201472
Unicodelet a = "\u{103}"
let sh = "\u{15F}"
"ă"
"ş"
let cedilla = "\u{327}" "̧"
Extended grapheme cluster
let sh2 = "\u{73}\u{327}"
"\u{15F}" == "\u{73}\u{327}" true
ș = s + cedilla
"ş"
Module 02 Slide Hello24 Ltd. (c) 201473
Unicode
let sh = "\u{15F}" "ş"
let realSh = "\u{219}" "ș"
9. Playgrounds
Slide Hello24 Ltd. (c) 201476
New Playground
Slide Hello24 Ltd. (c) 201477
Empty Playground
Slide Hello24 Ltd. (c) 201478
Playground
Slide Hello24 Ltd. (c) 201479
Playground
Slide Hello24 Ltd. (c) 201480
Fibonacci
Slide Hello24 Ltd. (c) 201481
Fibonacci
Slide Hello24 Ltd. (c) 201482
Fibonacci
Slide Hello24 Ltd. (c) 201483
Playground - Timeline
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
10. Swift REPL
Slide Hello24 Ltd. (c) 201486
REPLRead–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
Slide Hello24 Ltd. (c) 201487
man swift
Slide Hello24 Ltd. (c) 201488
swift -- help
Slide Hello24 Ltd. (c) 2014
$ 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
89
REPL
Slide Hello24 Ltd. (c) 2014
‣ 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
90
Resources
Slide Hello24 Ltd. (c) 2014
‣ 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
91
Resources
Slide Hello24 Ltd. (c) 2014
1.No C
2.Modern
3.Safety
4.Optionals
5.Mix & match
92
10 reasons you’ll love Swift
6.Functions - 1st class citizens
7.Magic
8.Unicode
9.Playgrounds
10.REPL
Objective-C ➾ Swift
Slide Hello24 Ltd. (c) 201494
One more thing…
WatchKit