Download - How would you describe Swift in three words?
![Page 1: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/1.jpg)
Intermediate Swift
@ColinEberhardt ShinobiControls
![Page 2: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/2.jpg)
![Page 3: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/3.jpg)
How would you describe Swift in three words?
![Page 4: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/4.jpg)
JavaBoringSimpleGang of Four
![Page 5: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/5.jpg)
C#ExpressiveFunJava++
![Page 6: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/6.jpg)
JavaScriptRapidSimpleBroken!
![Page 7: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/7.jpg)
Objective-CPowerfulUgly
![Page 8: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/8.jpg)
Swift????
![Page 9: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/9.jpg)
Swift hates nil
![Page 10: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/10.jpg)
Value Types
Reference Types
nill-able references
Objective-C ✓ ✓ ✓Java ✓ ✓ ✓
JavaScript ✓ ✓ ✓C# ✓ ✓ ✓
Swift ✓ ✓ ✗
†
†
† Non extensible
![Page 11: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/11.jpg)
nil is a bad thing
![Page 12: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/12.jpg)
RACSignal *signal = [[self.services getFlickrSearchService] flickrImageMetadata:self.photo.identifier];
![Page 13: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/13.jpg)
RACSignal *signal; if (self.services && self.photo && self.photo.identifier) { id<RWTFlickrSearch> searchService = [self.services getFlickrSearchService]; if (searchService) { signal = [searchService flickrImageMetadata:self.photo.identifier]; } }
RACSignal *signal = [[self.services getFlickrSearchService] flickrImageMetadata:self.photo.identifier];
![Page 14: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/14.jpg)
Opt-in with Optionals
![Page 15: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/15.jpg)
var person: String? !!var car: String? = "Porsche"
![Page 16: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/16.jpg)
var car: String? // car is nil car = "Porsche" !// checked if let actuallyACar = car { if actuallyACar.hasPrefix("P") { println("It's a 'P'") } } !// unchecked // fatal error: unexpectedly found nil while // unwrapping an Optional value if car!.hasPrefix("P") { println("It's a 'P'") } !// chaining if let hasPrefix = car?.hasPrefix("P") { if hasPrefix { println("It's a 'P'") } }
![Page 17: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/17.jpg)
Swift likes sugar
![Page 18: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/18.jpg)
var car: String? !!var car: Optional<String> = "Porsche"
![Page 19: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/19.jpg)
enum Optional<T> : Reflectable, NilLiteralConvertible { case None case Some(T) init() var hasValue: Bool { get } ! static func convertFromNilLiteral() -> T? }
![Page 20: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/20.jpg)
// sugar free var car: Optional<String> = "Porsche" !switch car { case .None: println("nothing here") case .Some(let actuallyACar): if actuallyACar.hasPrefix("P") { println("It's a 'P'") } }
// checked if let actuallyACar = car { if actuallyACar.hasPrefix("P") { println("It's a 'P'") } }
![Page 21: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/21.jpg)
Opt-out of unwrapping with implicit optionals
![Page 22: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/22.jpg)
var car: String! = "Porsche" !// unchecked - minus the unwrapping if car.hasPrefix("P") { println("It's a 'P'") }
![Page 23: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/23.jpg)
Swift meets Objective-C
![Page 24: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/24.jpg)
Objective-C permits nil
… as a result anything returned by the APIs could be a nil reference
![Page 25: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/25.jpg)
AnyObject!
![Page 26: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/26.jpg)
Variables are always surprised! or confused?
![Page 27: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/27.jpg)
Swift initialisation is strict
![Page 28: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/28.jpg)
Swift initialisation is really strict
![Page 29: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/29.jpg)
The Commandments
![Page 30: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/30.jpg)
Thou shalt initialise all properties of your type!Thou shalt not call super init before properties are initialised!
Thou shalt not call class methods until all properties are initialised!Thou shalt not use super properties before super.init!
Thou shalt only call designated initialisers on the superclass!Thou shalt not call super initialisers from convenience initialisers!
Thou shalt not call class methods before super.init !Thou shalt not kill
![Page 31: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/31.jpg)
Thou shalt initialise all properties of your type!Thou shalt not call super init before properties are initialised!
Thou shalt not call class methods until all properties are initialised!Thou shalt not use super properties before super.init!
Thou shalt only call designated initialisers on the superclass!Thou shalt not call super initialisers from convenience initialisers!
Thou shalt not call class methods before super.init !Thou shalt not kill
![Page 32: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/32.jpg)
Thou shalt initialise all properties of your type!Thou shalt not call super init before properties are initialised!
Thou shalt not call class methods until all properties are initialised!Thou shalt not use super properties before super.init!
Thou shalt only call designated initialisers on the superclass!Thou shalt not call super initialisers from convenience initialisers!
Thou shalt not call class methods before super.init !Thou shalt not kill
![Page 33: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/33.jpg)
Thou shalt initialise all properties of your type!Thou shalt not call super init before properties are initialised!
Thou shalt not call class methods until all properties are initialised!Thou shalt not use super properties before super.init!
Thou shalt only call designated initialisers on the superclass!Thou shalt not call super initialisers from convenience initialisers!
Thou shalt not call class methods before super.init !Thou shalt not kill
![Page 34: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/34.jpg)
Thou shalt initialise all properties of your type!Thou shalt not call super init before properties are initialised!
Thou shalt not call class methods until all properties are initialised!Thou shalt not use super properties before super.init!
Thou shalt only call designated initialisers on the superclass!Thou shalt not call super initialisers from convenience initialisers!
Thou shalt not call class methods before super.init !Thou shalt not kill
![Page 35: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/35.jpg)
Thou shalt initialise all properties of your type!Thou shalt not call super init before properties are initialised!
Thou shalt not call class methods until all properties are initialised!Thou shalt not use super properties before super.init!
Thou shalt only call designated initialisers on the superclass!Thou shalt not call super initialisers from convenience initialisers!
Thou shalt not call class methods before super.init !Thou shalt not kill
![Page 36: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/36.jpg)
Thou shalt initialise all properties of your type!Thou shalt not call super init before properties are initialised!
Thou shalt not call class methods until all properties are initialised!Thou shalt not use super properties before super.init!
Thou shalt only call designated initialisers on the superclass!Thou shalt not call super initialisers from convenience initialisers!
Thou shalt not call class methods before super.init !Thou shalt not kill
![Page 37: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/37.jpg)
Thou shalt initialise all properties of your type!Thou shalt not call super init before properties are initialised!
Thou shalt not call class methods until all properties are initialised!Thou shalt not use super properties before super.init!
Thou shalt only call designated initialisers on the superclass!Thou shalt not call super initialisers from convenience initialisers!
Thou shalt not call class methods before super.init !Thou shalt not kill
![Page 38: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/38.jpg)
class Cat: Animal { ... init(name: String, isLongHaired: Bool) { // initialize *all* class properties // no calls to 'self' methods here super.init(name: name) // perform initialisation that requires self & super // possibly update superclass properties } convenience init() { // can only call designated initializers on self self.init(name: "kitty", isLongHaired: false) } }
![Page 39: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/39.jpg)
![Page 40: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/40.jpg)
Strictness bites
![Page 41: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/41.jpg)
import Foundation !class Animal { let name: String init(name: String) { self.name = name.capitalizedString } } !class Cat: Animal { let catName: String override init(name: String) { super.init(name: name) ! // error: property 'self.catName' not // initialized at super.init call self.catName = self.name + "-kins" } }
![Page 42: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/42.jpg)
Solution - make catName surprised!
class Cat: Animal { let catName: String! override init(name: String) { super.init(name: name) self.catName = self.name + "-kins" } }
![Page 43: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/43.jpg)
Spot the semicolon!
![Page 44: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/44.jpg)
Swift is a bit muddled up!
![Page 45: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/45.jpg)
!class Cat { ... init(name: String) { ... } func updateWithName(name: String, isLongHaired: Bool) { ... } } !func updateTheCatsName(cat: Cat, newName: String, isLongHaired: Bool) { ... } !!// initializers - named parameters let cat = Cat(name: "molly") !// global functions - no named parameters updateTheCatsName(cat, "frank", true) !// methods - second and subsequent parameters are named cat.updateWithName("frank", isLongHaired: true); !
![Page 46: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/46.jpg)
!class Cat { ... init(_ name: String) { ... } func updateWithName(#name: String, isLongHaired: Bool) { ... } } !func updateTheCatsName(meow cat: Cat, newName: String, isLongHaired: Bool) { ... } !!// initializers let cat = Cat("molly") !// global functions updateTheCatsName(meow: cat, "frank", true) !// methods cat.updateWithName(name: "frank", isLongHaired: true)
![Page 47: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/47.jpg)
Delegation patternclass ViewController: UIViewController, UITableViewDelegate { ! ... ! func tableView(tableView: UITableView!, didDeselectRowAtIndexPath indexPath: NSIndexPath!) { } func tableView(tableView: UITableView!, didEndDisplayingFooterView view: UIView!, forSection section: Int) { } ! func tableView(tableView: UITableView!, canPerformAction action: Selector, forRowAtIndexPath indexPath: NSIndexPath!, withSender sender: AnyObject!) -> Bool { } }
![Page 48: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/48.jpg)
Method naming
tableView:canPerformAction:forRowAtIndexPath:withSender:
tableView(_:canPerformAction:forRowAtIndexPath:withSender:)
![Page 49: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/49.jpg)
Swift likes immutability!
![Page 50: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/50.jpg)
Immutability - C#
const
readonly
ReadOnlyCollection
ArrayList
![Page 51: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/51.jpg)
Immutability - Objective-C
const
#define
NSArray
NSMutableArray
![Page 52: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/52.jpg)
Immutability is a first-class concept in Swift
var is a variable, it can be re-assigned
let is a constant, it cannot be re-assigned
![Page 53: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/53.jpg)
Constants are everywhere
for i in 0...5 { // error i = 27 } !!!func square(number: Double) { // error number = number * number }
![Page 54: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/54.jpg)
Mutability in practice
![Page 55: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/55.jpg)
A variable class
class Coordinate { var x: Int = 0, y: Int = 0 } !var coord = Coordinate() coord.x = 56 !coord = Coordinate() coord.y = 57;
Class variable, you can change properties and re-assign
![Page 56: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/56.jpg)
A constant class (almost)
class Coordinate { var x: Int = 0, y: Int = 0 } !let coord = Coordinate() coord.x = 56 !// fails here coord = Coordinate() coord.y = 57
A constant class can be mutated
A constant class cannot be re-assigned
![Page 57: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/57.jpg)
A variable struct
struct Coordinate { var x: Int = 0, y: Int = 0 } !var coord = Coordinate() coord.x = 56 !coord = Coordinate() coord.y = 57
Variable struct, you can change properties and re-assign
Just like variable classes!
![Page 58: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/58.jpg)
Constant structs are immutable :-)
struct Coordinate { var x: Int = 0, y: Int = 0 } !let coord = Coordinate() // error: fails here coord.x = 56 !coord = Coordinate() coord.y = 57 !
Constant struct, you cannot mutate it!
This is super-awesome, by changing a keyword, you change the mutability of the entire object.
![Page 59: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/59.jpg)
Strings, Arrays and Dictionaries are structs
// a variable array var coord = [1,2,3,4] !// can be mutated! coord[1] = 5 // “[1, 4, 3, 4]”
// a constant array let coord = [1,2,3,4] !// error: ’@lvalue $T5' is not identical to 'Int' coord[1] = 5;
![Page 60: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/60.jpg)
Creating Immutable Types
struct Coordinate { var x = 0, y = 0 func transpose() { let temp = x // error: cannot assign to ‘x’ in ‘self’ x = y y = temp } }
![Page 61: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/61.jpg)
Mutating Functions
struct Coordinate { var x = 0, y = 0 mutating func transpose() { let temp = x // no error! x = y y = temp } }
![Page 62: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/62.jpg)
Mutating Functions
// a variable coordinate var coord = Coordinate() coord.transpose() !!!// a constant coordinate let constCoord = Coordinate() !// error: immutable value of type ‘Coordinate' // only has mutating members named 'transpose' constCoord.transpose()
![Page 63: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/63.jpg)
Structs are value-types
func trimArrayToLength<T>(array: [T], length: Int) { while array.count > length { // error: immutable value of type '[T]' only // has mutating members named 'removeLast' array.removeLast() } }
![Page 64: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/64.jpg)
Structs are value-types
func trimArrayToLength<T>(inout array: [T], length: Int) { while array.count > length { array.removeLast() } } !var myArray = [1,3,5,3,8,9] trimArrayToLength(&myArray, 3) println(myArray) // [1,3,5]
![Page 65: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/65.jpg)
Whenever you start typing var, type let instead, only changing it back to var if it does not compile!
![Page 66: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/66.jpg)
Swift is Functional
![Page 67: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/67.jpg)
First-class functionsimport Foundation !func square(number: Double) -> Double { return number * number } !let a = 3.0, b = 4.0 let c = sqrt(square(a) + square(b)) println(c) // 5.0
![Page 68: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/68.jpg)
import Foundation !func square(number: Double) -> Double { return number * number } !let operation = square !let a = 3.0, b = 4.0 let c = sqrt(operation(a) + operation(b)) println(c) // 5.0
![Page 69: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/69.jpg)
Function types
let operation = square
The type of operation is being inferred
But what *is* the type?
![Page 70: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/70.jpg)
Function types
let operation:(Double) -> Double = square
![Page 71: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/71.jpg)
Functions can be:
Assigned to variables or constants
Passed to / from other functions
![Page 72: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/72.jpg)
Fun with functionsfunc * (fn: () -> (), times: Int) { for _ in 0..<times { fn() } } !{ println("Swift rocks!") } * 3 !// Swift rocks! // Swift rocks! // Swift rocks!
http://ijoshsmith.com/2014/07/05/custom-threading-operator-in-swift/
![Page 73: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/73.jpg)
More functional fun …
![Page 74: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/74.jpg)
// non functional var evens = [Int]() for i in 1...10 { if i % 2 == 0 { evens += [i] } } !var evenSum = 0 for i in evens { evenSum += i }
// functional evenSum = Array(1...10) .filter { (number) in number % 2 == 0 } .reduce(0) { (total, number) in total + number }
the sum of all even numbers between 1 and 10
![Page 75: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/75.jpg)
Partial applicationfunc createSplitter(separator:String) -> (String -> [String]) { func split(source:String) -> [String] { return source.componentsSeparatedByString(separator) } return split }
let data = "5,7;3,4;55,6" let commaSplitter = createSplitter(",") commaSplitter(data) !let semiColonSplitter = createSplitter(";") semiColonSplitter(data)
![Page 76: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/76.jpg)
Curried functions
func createSplitter(separator:String)(source:String) -> [String] { return source.componentsSeparatedByString(separator) }
func createSplitter(separator:String) -> (String -> [String]) { func split(source:String) -> [String] { return source.componentsSeparatedByString(separator) } return split }
![Page 77: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/77.jpg)
class Person { let name: String; init(name: String) { self.name = name } func greeting() { println("Hello \(name)") } } !let speaker = Person(name: "Colin") speaker.greeting() // "Hello Colin" !let speakFunction = speaker.greeting speakFunction() // "Hello Colin" !let curriedFunc = Person.greeting curriedFunc(speaker)() // "Hello Colin"
![Page 78: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/78.jpg)
Swift is concise
![Page 79: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/79.jpg)
Sorting an array
let animals = ["fish", "cat" , "chicken", "dog"] !func isBefore(one: String, two:String) -> Bool { return one < two } !let sortedStrings = animals.sorted(isBefore) !println(sortedStrings) // [cat, chicken, dog, fish] !!
![Page 80: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/80.jpg)
Closure expressions as anonymous functions
let sorted = animals.sorted({ (one: String, two: String) -> Bool in return one > two })
![Page 81: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/81.jpg)
let sorted = animals.sorted({ (one: String, two: String) -> Bool in return one > two })
46 characters
![Page 82: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/82.jpg)
let sorted = animals.sorted({ (one, two) -> Bool in return one > two })
32 characters
![Page 83: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/83.jpg)
let sorted = animals.sorted({ (one, two) in return one > two })
26 characters
![Page 84: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/84.jpg)
let sorted = animals.sorted({ one, two in return one > two })
24 characters
![Page 85: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/85.jpg)
let sorted = animals.sorted({ one, two in one > two })
19 characters
![Page 86: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/86.jpg)
50% smaller
let sorted = animals.sorted({ one, two in one > two })
let sorted = animals.sorted({ (one: String, two: String) -> Bool in return one > two })
![Page 87: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/87.jpg)
let sorted = animals.sorted({ $0 > $1 })
7 characters
![Page 88: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/88.jpg)
let sorted = animals.sorted() { $0 > $1 }
7 characters
![Page 89: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/89.jpg)
let sorted = animals.sorted { $0 > $1 }
7 characters
![Page 90: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/90.jpg)
let sorted = animals.sorted(>)
1 character!
![Page 91: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/91.jpg)
Swift is clear
![Page 92: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/92.jpg)
Swift still uses ARC
![Page 93: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/93.jpg)
Swift still has retain cycles
![Page 94: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/94.jpg)
Objective-C syntax
__weak typeof(self)weakSelf = self; [self.context performBlock:^{ __strong typeof(weakSelf)strongSelf = weakSelf; // do something with strongSelf }];
![Page 95: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/95.jpg)
Swift syntax
closure = { [unowned self] () -> () in // do something with self }
![Page 96: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/96.jpg)
Swift is ‘open’
![Page 97: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/97.jpg)
for-in and sequenceclass Fibonacci: SequenceType { func generate() -> GeneratorOf<Int> { var current = 0, next = 1 return GeneratorOf<Int> { var ret = current current = next next = next + ret return ret } } } !for num in Fibonacci() { println(num) // 0, 1, 1, 2, 3, 5, 8, 13 } !
http://www.scottlogic.com/blog/2014/06/26/swift-sequences.html
![Page 98: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/98.jpg)
Literal conversionclass Person: StringLiteralConvertible { let name: String required init(name: String) { self.name = name } class func convertFromStringLiteral(value: StringLiteralType) -> Self { return self(name: value) } class func convertFromExtendedGraphemeClusterLiteral(value: String) -> Self { return self(name: value) } } !var people: [Person] = ["Bob", "Frank", "Brian"]
![Page 99: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/99.jpg)
How would you describe Swift in three words?
![Page 100: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/100.jpg)
Safe
![Page 101: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/101.jpg)
Swift hates nil Swift initialisation is really strict Swift likes immutable types
![Page 102: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/102.jpg)
Muddled
![Page 103: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/103.jpg)
Swift parameter naming is muddled up Swift variables are often dazed! or confused?
![Page 104: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/104.jpg)
Modern
![Page 105: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/105.jpg)
Swift is functional
Swift likes sugar
Swift is clear and concise
![Page 106: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/106.jpg)
SwiftSafeMuddledModern
![Page 107: How would you describe Swift in three words?](https://reader034.vdocument.in/reader034/viewer/2022051609/547a0bb9b4af9fce158b499d/html5/thumbnails/107.jpg)
Swift wish-list
Some form of reflection
Exceptions (try / catch / throw)
A re-write of Foundation, UIKit, CoreData …