CuriosMinds, Brasov, Sept 18th 2014 Slide 2
CuriosMinds, Brasov, Sept 18th 2014 Slide 3
CuriosMinds, Brasov, Sept 18th 2014 Slide 3
An Android, an iOS, a Blackberry and a Windows Mobile developer walk into a bar…
CuriosMinds, Brasov, Sept 18th 2014 Slide 3
An Android, an iOS, a Blackberry and a Windows Mobile developer walk into a bar…
The barman looks at them & says…
CuriosMinds, Brasov, Sept 18th 2014 Slide 3
An Android, an iOS, a Blackberry and a Windows Mobile developer walk into a bar…
The barman looks at them & says…
Gee, it must be in town
To swiftly go where no other language has
gone before
Good artists copy, Real artists steal
Everything is a remix
https://www.flickr.com/photos/mugley/800999028/
The talk about that new programming language
everyone talks about but nobody really uses…
Yet !
Everything is a remix
https://www.flickr.com/photos/mugley/800999028/
CuriosMinds, Brasov, Sept 18th 2014 Slide 12
CuriosMinds, Brasov, Sept 18th 2014 Slide 12
CuriosMinds, Brasov, Sept 18th 2014 Slide 13
CuriosMinds, Brasov, Sept 18th 2014 Slide 13
#freeJonyIve
CuriosMinds, Brasov, Sept 18th 2014 Slide 14
It’s all about the users
https://www.flickr.com/photos/wyogirl13/6455191187
It’s all about the users.
https://www.flickr.com/photos/wyogirl13/6455191187
It’s all about the users.
https://www.flickr.com/photos/wyogirl13/6455191187
Why?
Why?What?
Why?What?When?
Why?What?When?How?
Why?
CuriosMinds, Brasov, Sept 18th 2014 Slide 24
CuriosMinds, Brasov, Sept 18th 2014 Slide 25
AAPL
CuriosMinds, Brasov, Sept 18th 2014 Slide 26
Products
CuriosMinds, Brasov, Sept 18th 2014 Slide 27
Software
CuriosMinds, Brasov, Sept 18th 2014 Slide 28
App Stores
CuriosMinds, Brasov, Sept 18th 2014 Slide 29
Apple needs developers
CuriosMinds, Brasov, Sept 18th 2014 Slide
‣ maximise the number of developers
‣ keeping existing developers happy
31
2 more things…
CuriosMinds, Brasov, Sept 18th 2014 Slide
‣ 30 year old language
‣ drastically different from other languages
‣ not entirely future-proof
32
Objective-C
What?
Apps For Good, London, June 29th 2012 Slide Hello24 Ltd. (c) 201234
LLVM & Clang
Apps For Good, London, June 29th 2012 Slide Hello24 Ltd. (c) 201234
LLVM & Clang
gcc => llvm-gcc => llvm
Swift
CuriosMinds, Brasov, Sept 18th 2014 Slide
Objective-C without the C
36
Swift
CuriosMinds, Brasov, Sept 18th 2014 Slide
Objective-C without the C
36
Objective-C is to Swift <=> cat is to cattle
Swift
Swift
Safety
CuriosMinds, Brasov, Sept 18th 2014 Slide 39
Safety - constants vs variables
CuriosMinds, Brasov, Sept 18th 2014 Slide 39
Safety - constants vs variables
let theAnswer = 42
CuriosMinds, Brasov, Sept 18th 2014 Slide 39
Safety - constants vs variables
let theAnswer = 42
var numberBooks = 1
CuriosMinds, Brasov, Sept 18th 2014 Slide 39
Safety - constants vs variables
let theAnswer = 42
var numberBooks = 1
var numberBooks: Int = 1
CuriosMinds, Brasov, Sept 18th 2014 Slide 40
Safety - no implicit conversion
CuriosMinds, Brasov, Sept 18th 2014 Slide 40
Safety - no implicit conversion
var numberBooks = 1
CuriosMinds, Brasov, Sept 18th 2014 Slide 40
Safety - no implicit conversion
var numberBooks = 1
numberBooks = 2.5
CuriosMinds, Brasov, Sept 18th 2014 Slide 40
Safety - no implicit conversion
var numberBooks = 1
numberBooks = 2.5 ❗️
CuriosMinds, Brasov, Sept 18th 2014 Slide 40
Safety - no implicit conversion
var numberBooks = 1
numberBooks = 2.5 ❗️
numberBooks = Int(2.5)
CuriosMinds, Brasov, Sept 18th 2014 Slide 40
Safety - no implicit conversion
var numberBooks = 1
numberBooks = 2.5 ❗️
numberBooks = Int(2.5) ✔
CuriosMinds, Brasov, Sept 18th 2014 Slide 41
Safety - switch
switch index { case 1: println("first") case 2, 3: println("second or third") case 4...10: println("top 10") default: println("others...") }
New idioms
CuriosMinds, Brasov, Sept 18th 2014 Slide 43
Tuples
CuriosMinds, Brasov, Sept 18th 2014 Slide 43
Tuples
let http404Error = (404, "Not Found")
CuriosMinds, Brasov, Sept 18th 2014 Slide 43
Tuples
let x = 1 let y = 2
let point = (x, y)
let http404Error = (404, "Not Found")
CuriosMinds, Brasov, Sept 18th 2014 Slide 43
Tuples
let x = 1 let y = 2
let point = (x, y)
point.0
let http404Error = (404, "Not Found")
CuriosMinds, Brasov, Sept 18th 2014 Slide 43
Tuples
let x = 1 let y = 2
let point = (x, y)
point.0
let origin = (x: 200, y: 100) origin.y
let http404Error = (404, "Not Found")
CuriosMinds, Brasov, Sept 18th 2014 Slide 44
Tuples
CuriosMinds, Brasov, Sept 18th 2014 Slide 44
Tuples// Original var fibonacci = 1 var prev = 0
while fibonacci < 100 { var prev_tmp = fibonacci fibonacci += prev prev = prev_tmp println(fibonacci) }
CuriosMinds, Brasov, Sept 18th 2014 Slide 44
Tuples// Original var fibonacci = 1 var prev = 0
while fibonacci < 100 { var prev_tmp = fibonacci fibonacci += prev prev = prev_tmp println(fibonacci) }
// tuples var fibonacci_t = 1 var prev_t = 0
while fibonacci_t < 100 { (prev_t, fibonacci_t) = (fibonacci_t, fibonacci_t + prev_t) println(fibonacci_t) }
Functions as 1st class citizens
CuriosMinds, Brasov, Sept 18th 2014 Slide 46
Functions
func 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
CuriosMinds, Brasov, Sept 18th 2014 Slide 46
Functions
func 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
CuriosMinds, Brasov, Sept 18th 2014 Slide 46
Functions
func 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
CuriosMinds, Brasov, Sept 18th 2014 Slide 46
Functions
func 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
CuriosMinds, Brasov, Sept 18th 2014 Slide 46
Functions
func 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")
CuriosMinds, Brasov, Sept 18th 2014 Slide 46
Functions
func 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"
CuriosMinds, Brasov, Sept 18th 2014 Slide 47
Closures
CuriosMinds, Brasov, Sept 18th 2014 Slide 47
Closuresfunc greetWithMessage(name: String, message: (String) -> String) -> String { return message(name); }
CuriosMinds, Brasov, Sept 18th 2014 Slide 47
Closuresfunc greetWithMessage(name: String, message: (String) -> String) -> String { return message(name); }
greetWithMessage("Paul", { (name: String) -> String in return "Good morning \(name)" } )
CuriosMinds, Brasov, Sept 18th 2014 Slide 47
Closuresfunc 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)" })
CuriosMinds, Brasov, Sept 18th 2014 Slide 47
Closuresfunc 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") { name in return "Good morning \(name)" }
CuriosMinds, Brasov, Sept 18th 2014 Slide 47
Closuresfunc 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") { name in return "Good morning \(name)" }
greetWithMessage("Paul") { return "Good morning \($0)" }
CuriosMinds, Brasov, Sept 18th 2014 Slide 48
Tools
Playgrounds
Module 01 Slide Hello24 Ltd. (c) 201452
New Playground
Module 01 Slide Hello24 Ltd. (c) 201453
Empty Playground
Module 01 Slide Hello24 Ltd. (c) 201454
Playground
Module 01 Slide Hello24 Ltd. (c) 201455
Playground
Module 01 Slide Hello24 Ltd. (c) 201455
Playground
Module 01 Slide Hello24 Ltd. (c) 201455
Playground
Module 01 Slide Hello24 Ltd. (c) 201456
Fibonacci
Module 01 Slide Hello24 Ltd. (c) 201457
Fibonacci
Module 01 Slide Hello24 Ltd. (c) 201458
Fibonacci
Module 01 Slide Hello24 Ltd. (c) 201459
Playground - Timeline
Module 01 Slide Hello24 Ltd. (c) 201460
Playgrounds
Module 01 Slide Hello24 Ltd. (c) 2014
‣ Interactive experience
‣ Immediate feedback
‣ Watch code progression through loops
60
Playgrounds
Module 01 Slide Hello24 Ltd. (c) 2014
‣ Interactive experience
‣ Immediate feedback
‣ Watch code progression through loops
‣ Easy way to
‣ prototype
‣ test snippets of code
60
Playgrounds
Module 01 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.
60
Playgrounds
Swift REPL
Module 01 Slide Hello24 Ltd. (c) 201462
REPLRead–eval–print loop
Module 01 Slide Hello24 Ltd. (c) 201462
REPL
$ which swift
/usr/bin/swift
Read–eval–print loop
Module 01 Slide Hello24 Ltd. (c) 201462
REPL
$ which swift
/usr/bin/swift
$ swift -v
Swift version 1.0 (swift-600.0.51.3)
Target: x86_64-apple-darwin14.0.0
<unknown>:0: error: the SDK 'MacOSX10.9.sdk' does not
support Swift
Read–eval–print loop
Module 01 Slide Hello24 Ltd. (c) 201463
man swift
Module 01 Slide Hello24 Ltd. (c) 201464
REPL
Module 01 Slide Hello24 Ltd. (c) 201464
REPL$ swift -v
…
<unknown>:0: error: the SDK 'MacOSX10.9.sdk' does not support Swift
Module 01 Slide Hello24 Ltd. (c) 201464
REPL$ swift -v
…
<unknown>:0: error: the SDK 'MacOSX10.9.sdk' does not support Swift
Module 01 Slide Hello24 Ltd. (c) 201464
REPL$ swift -v
…
<unknown>:0: error: the SDK 'MacOSX10.9.sdk' does not support Swift
Module 01 Slide Hello24 Ltd. (c) 201465
REPL
Module 01 Slide Hello24 Ltd. (c) 201465
REPL$ swift -v
Module 01 Slide Hello24 Ltd. (c) 201465
REPL$ swift -v
Swift version 1.1 (swift-600.0.54.4) Target: x86_64-apple-darwin14.0.0 /Applications/Xcode-Beta.app/Contents/Developer/usr/bin/lldb "--repl=-target x86_64-apple-darwin14.0.0 -target-cpu core2 -sdk /Applications/Xcode-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -color-diagnostics"
Module 01 Slide Hello24 Ltd. (c) 201465
REPL$ swift -v
Swift version 1.1 (swift-600.0.54.4) Target: x86_64-apple-darwin14.0.0 /Applications/Xcode-Beta.app/Contents/Developer/usr/bin/lldb "--repl=-target x86_64-apple-darwin14.0.0 -target-cpu core2 -sdk /Applications/Xcode-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -color-diagnostics"
$ swift
Welcome to Swift! Type :help for assistance.
Module 01 Slide Hello24 Ltd. (c) 201466
REPL
$ 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
When?
CuriosMinds, Brasov, Sept 18th 2014 Slide
‣ 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
68
Now!
CuriosMinds, Brasov, Sept 18th 2014 Slide 69
Resources
CuriosMinds, Brasov, Sept 18th 2014 Slide 70
Resources
CuriosMinds, Brasov, Sept 18th 2014 Slide 72
How?
CuriosMinds, Brasov, Sept 18th 2014 Slide 74
Swift project
CuriosMinds, Brasov, Sept 18th 2014 Slide 75
Bridging
CuriosMinds, Brasov, Sept 18th 2014 Slide 76
Bridging
CuriosMinds, Brasov, Sept 18th 2014 Slide 77
// // STDataObject.swift //
import Foundation import CoreData
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 }
…
}
CuriosMinds, Brasov, Sept 18th 2014 Slide 78
CuriosMinds, Brasov, Sept 18th 2014 Slide 78
CuriosMinds, Brasov, Sept 18th 2014 Slide 79
CuriosMinds, Brasov, Sept 18th 2014 Slide 79
Objective-C ➾ Swift
CuriosMinds, Brasov, Sept 18th 2014 Slide 81
Fizz Buzz classic
CuriosMinds, Brasov, Sept 18th 2014 Slide 81
Fizz Buzz classic
let fizzBuzz = ({ (n: Int) -> String in if( 0 == (n % 3 + n % 5)) { return "FizzBuzz" } if( 0 == n % 3) { return "Fizz" } if( 0 == n % 5) { return "Buzz" } else { return "\(n)" } })
fizzBuzz(1) fizzBuzz(2) fizzBuzz(3) fizzBuzz(5) fizzBuzz(9) fizzBuzz(10) fizzBuzz(15)
CuriosMinds, Brasov, Sept 18th 2014 Slide 81
Fizz Buzz classic
let fizzBuzz = ({ (n: Int) -> String in if( 0 == (n % 3 + n % 5)) { return "FizzBuzz" } if( 0 == n % 3) { return "Fizz" } if( 0 == n % 5) { return "Buzz" } else { return "\(n)" } })
fizzBuzz(1) fizzBuzz(2) fizzBuzz(3) fizzBuzz(5) fizzBuzz(9) fizzBuzz(10) fizzBuzz(15)
“1”“2”“fizz”“buzz”“fizz”“buzz”“fizzbuzz”
CuriosMinds, Brasov, Sept 18th 2014 Slide 82
Swift Fizz Buzz
CuriosMinds, Brasov, Sept 18th 2014 Slide 82
Swift Fizz Buzzlet swiftFizzBuzz = ({ (aNumber: Int) -> String in switch (aNumber % 3, aNumber % 5 ) { case (0, 0): return "FizzBuzz" case (0, _): return "Fizz" case (_, 0): return "Buzz" default: return "\(aNumber)" } })
swiftFizzBuzz(1) swiftFizzBuzz(2) swiftFizzBuzz(3) swiftFizzBuzz(5) swiftFizzBuzz(9) swiftFizzBuzz(10) swiftFizzBuzz(15)
CuriosMinds, Brasov, Sept 18th 2014 Slide 82
Swift Fizz Buzzlet swiftFizzBuzz = ({ (aNumber: Int) -> String in switch (aNumber % 3, aNumber % 5 ) { case (0, 0): return "FizzBuzz" case (0, _): return "Fizz" case (_, 0): return "Buzz" default: return "\(aNumber)" } })
swiftFizzBuzz(1) swiftFizzBuzz(2) swiftFizzBuzz(3) swiftFizzBuzz(5) swiftFizzBuzz(9) swiftFizzBuzz(10) swiftFizzBuzz(15)
“1”“2”“fizz”“buzz”“fizz”“buzz”“fizzbuzz”
CuriosMinds, Brasov, Sept 18th 2014 Slide 83
Fizz Buzz - side by sidelet fizzBuzz = ({ (n: Int) -> String in if( 0 == (n % 3 + n % 5)) { return "FizzBuzz" } if( 0 == n % 3) { return "Fizz" } if( 0 == n % 5) { return "Buzz" } else { return "\(n)" } })
let swiftFizzBuzz = ({ (aNumber: Int) -> String in switch (aNumber % 3, aNumber % 5 ) { case (0, 0): return "FizzBuzz" case (0, _): return "Fizz" case (_, 0): return "Buzz" default: return "\(aNumber)" } })
Objective-C ➾ Swift
CuriosMinds, Brasov, Sept 18th 2014 Slide 85
Recommended