$30 off During Our Annual Pro Sale. View details »

Fun fact about Swift

Sash Zats
November 23, 2014

Fun fact about Swift

Got an invitation to a swift cocktail party? Don't worry, these flashcards will help you to keep up with conversations around you (most of it is probably invalid by the time you read it or won;t even compile)

Sash Zats

November 23, 2014
Tweet

More Decks by Sash Zats

Other Decks in Technology

Transcript

  1. Swift flashcards A guide to success at cocktail parties Sash

    Zats - zats.io - @zats - September 8, Tel Aviv
  2. [self helloAgain:AAPLAudienceTypeSwiftLovers];

  3. [self helloAgain:AudienceTypeSwiftLovers];

  4. self.helloAgain(AudienceTypeSwiftLovers)

  5. self.helloAgain(.SwiftLovers)

  6. 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!
  7. KVO

  8. KVO-ish

  9. KVO-ish Take action while property setter is in flight var

    pet: Pet { willSet (newPet) { // cleanup } didSet { // awesome } }
  10. Functions overloading Same function name, different signatures func add(a: Int,

    b: Int) -> Int func add(a: String, b: String) -> String
  11. Operator overloading

  12. Operator?

  13. 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
  14. Optional chaining I don't always unwrap, but when I do…

    let y: SomeClass? = nil let z = y?.someMethod() // will produce nil
  15. 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…
  16. siwtch statments switch size { case "a", "b": println("Small") case

    "c"..."e": println("Medium") case "f"..<"e": println("Large") default: println("Run!!") }
  17. Syntactic sweetness • Trailing closures dispatch_async(queue) { println("dispatch!") } •

    Freaking emoji in variables! let !" = "Moof" No emoji in operators !
  18. Optional Bools var b: Bool? if let b = b

    { if (b) { println("YES") } else { println("NO") } } else { println("Don't know") }
  19. 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
  20. 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)
  21. Thanks func memoize<T: Hashable, U>( body: ((T)->U, T)->U ) ->

    (T)->U { var memo = Dictionary<T, U>() 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) }
  22. Stop reading NSHipster, all the cool kids are at dev

    forums!