Slide 1

Slide 1 text

Swift flashcards A guide to success at cocktail parties Sash Zats - zats.io - @zats - September 8, Tel Aviv

Slide 2

Slide 2 text

[self helloAgain:AAPLAudienceTypeSwiftLovers];

Slide 3

Slide 3 text

[self helloAgain:AudienceTypeSwiftLovers];

Slide 4

Slide 4 text

self.helloAgain(AudienceTypeSwiftLovers)

Slide 5

Slide 5 text

self.helloAgain(.SwiftLovers)

Slide 6

Slide 6 text

Design • Small runtime (~5Mb? Pff), deployable to the previous OSes • Statically compiled, no JIT, garbage collection… duh… • No legacy, no abstraction penalties: Ints & Floats are structs • ARC • Multithreading built into language (aka atomic). NO!

Slide 7

Slide 7 text

KVO

Slide 8

Slide 8 text

KVO-ish

Slide 9

Slide 9 text

KVO-ish Take action while property setter is in flight var pet: Pet { willSet (newPet) { // cleanup } didSet { // awesome } }

Slide 10

Slide 10 text

Functions overloading Same function name, different signatures func add(a: Int, b: Int) -> Int func add(a: String, b: String) -> String

Slide 11

Slide 11 text

Operator overloading

Slide 12

Slide 12 text

Operator?

Slide 13

Slide 13 text

Typed collections var bag: Cat[] = [ snowball, oliver, jasper ] bag.append(scoobyDoo) // compiler error var mapping: [Bool: String] = Dictionary() mapping[true] = "true" mapping[true]!.utf8 // don't forget to unwrap mapping[false] = 1 // compiler error

Slide 14

Slide 14 text

Optional chaining I don't always unwrap, but when I do… let y: SomeClass? = nil let z = y?.someMethod() // will produce nil

Slide 15

Slide 15 text

Mutable, immutable collections var strings: [String] = [ "a", "b", "c" ] strings.append("d") let iStrings = string iStrings.append("e") // compiler error let iInts: [Int] = [ 1, 2, 3 ] var ints = iInts ints.append("d") // hmmm…

Slide 16

Slide 16 text

siwtch statments switch size { case "a", "b": println("Small") case "c"..."e": println("Medium") case "f"..<"e": println("Large") default: println("Run!!") }

Slide 17

Slide 17 text

Syntactic sweetness • Trailing closures dispatch_async(queue) { println("dispatch!") } • Freaking emoji in variables! let !" = "Moof" No emoji in operators !

Slide 18

Slide 18 text

Optional Bools var b: Bool? if let b = b { if (b) { println("YES") } else { println("NO") } } else { println("Don't know") }

Slide 19

Slide 19 text

Runtime • Compatible Mach-O binaries, ≈ Objective C • No dynamic lookup: virtual tables (isa → isa → … SwiftObject ), "Protocol witness table" • Devirtualization: no subclasses, @final • Meta programming only through @objc (no Mantle, no swizzling) • struct's functions

Slide 20

Slide 20 text

Uniqueness • Modules • Name mangling • MyClass & Model gone wild • LLDB can use modules instead of DWARF to understand types, including "not included" generics! • xcrun swift-demangle _TF5MyApp6myFuncFTSiSi_TSS_ ! MyApp.myFunc(Int, Int) -> (String)

Slide 21

Slide 21 text

Thanks func memoize( body: ((T)->U, T)->U ) -> (T)->U { var memo = Dictionary() var result: ((T)->U)! result = { x in if let q = memo[x] { return q } let r = body(result, x) memo[x] = r return r } return result } let factorial = memoize { factorial, x in x == 0 ? 1 : x * factorial(x - 1) }

Slide 22

Slide 22 text

Stop reading NSHipster, all the cool kids are at dev forums!