Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift, Briskly

Ash Furrow
November 03, 2016

Swift, Briskly

Presented at try! Swift NYC Meetup: https://www.meetup.com/try_SwiftNYC/events/234789872/

Ash Furrow

November 03, 2016
Tweet

More Decks by Ash Furrow

Other Decks in Programming

Transcript

  1. Swift, Briskly
    Ash Furrow, Artsy

    View full-size slide

  2. Today’s Goal:
    To help you fake your way through a job interview

    View full-size slide

  3. Agenda
    1. The history of Swift
    2. How Swift became open source
    3. Swift syntax

    View full-size slide

  4. First, we need to talk about
    Objective-C

    View full-size slide

  5. Objective-C has advanced as far
    as it can

    View full-size slide

  6. Replacing Objective-C was a
    strategic necessity

    View full-size slide

  7. Requirements for an Objective-C replacement
    1. No pointers
    2. Managed memory
    3. Native unicode support
    4. Concise
    5. Named parameters

    View full-size slide

  8. Swift expectations

    View full-size slide

  9. Open Source
    Swift

    View full-size slide

  10. Swift Evolution

    View full-size slide

  11. bugs.swift.org

    View full-size slide

  12. Swift on
    Linux

    View full-size slide

  13. Swift is a multi-purpose language

    View full-size slide

  14. Swift is
    multi-platform
    and
    multi-paradigm

    View full-size slide

  15. Types
    A type is a classification of data which tells the
    computer how the programmer intends to use that
    data.
    — Wikipedia
    So basically programmers tell the computer their
    intentions and the computer will complain if the
    programmer fails to adhere to them.

    View full-size slide

  16. Types
    Example types:
    —Integers
    —Booleans
    —Functions
    —Classes
    Swift is strongly-typed.

    View full-size slide

  17. Built-in Types
    —String (ex: "Hello, world!")
    —Int (ex: 1234 or -1)
    —Bool (ex: true or false)
    —etc...

    View full-size slide

  18. Enums
    An enum is a type with mutually-exclusive cases.
    enum ArtworkAvailability {
    case notForSale
    case forSale
    case onHold
    case sold
    }
    ArtworkAvailability.forSale

    View full-size slide

  19. Emoji
    Swift has full unicode support, so you can make
    enum cases have emoji, if you want to.
    enum Animals {
    case !
    case "
    case #
    }

    View full-size slide

  20. Enums
    Enums are super-powerful. Here’re two examples:
    enum Bool {
    case true
    case false
    }
    enum Never {
    }

    View full-size slide

  21. Arrays
    Arrays are variable-length data containers for
    homogenous data.
    ["Orta", "Ash", "Eloy", "Sarah", "Maxim"]
    [1, 200, -5, 73]
    [ArtworkAvailability.forSale, .onHold, .sold]

    View full-size slide

  22. Variables
    Variables are a place to store data and refer to it by
    name. Variables in Swift are defined one of two
    ways:
    —var variables whose values can change.
    —let constants whose values can never change.
    Swift generally prefers let. If you use var when you
    don't need it, the compiler will suggest using let
    instead.

    View full-size slide

  23. Type Inference
    Swift is a strongly-typed language, but compare
    these two equivalent lines of code:
    let array: [String] = ["Ash", "Orta", "Sarah"]
    ...
    let array = ["Ash", "Orta", "Sarah"]
    In either case, Swift needs to disambiguate what is
    stored in the array.

    View full-size slide

  24. Tuples
    Tuples are fixed-length data containers for
    heterogenous data.
    let presenter = ("Ash", [Animal.!, .!])
    presenter.0 // "Ash"
    presenter.1 // [Animal.!, .!]
    presenter.2 // compiler error!

    View full-size slide

  25. Named Tuples
    Swift also supports named tuples to access
    elements at specific positions in a tuple.
    let presenter = (name: "Ash", pets: [Animal.!, .!])
    presenter.name // "Ash"
    presenter.pets // [Animal.!, .!]

    View full-size slide

  26. Functions
    Functions are pieces of code. They can optionally
    accept input and/or return output.
    func sayHi() {
    print("Hello, world!")
    }
    sayHi()

    View full-size slide

  27. Functions
    func say(message: String) {
    print(message)
    }
    say(message: "Hello, world!")

    View full-size slide

  28. Functions
    func greet(name: String) -> String {
    return "Hello, " + name
    }
    print(greet(name: "Ash")) // prints "Hello, Ash"

    View full-size slide

  29. Closures
    [1, 2, 3, 4, 5].filter({ number in
    return (number % 2 == 0)
    }) // Returns [2, 4]
    [1, 2, 3, 4, 5].filter { return ($0 % 2 == 0) }
    [1, 2, 3, 4, 5].filter { $0 % 2 == 0 }

    View full-size slide

  30. Classes
    Classes contain data and are passed by reference.
    class Person {
    let name: String
    let dateOfBirth: Date
    ...
    }
    let person = Person(name: "Ash", dateOfBirth: ...

    View full-size slide

  31. Structs
    Structs contain data and are passed by value.
    struct Person {
    let name: String
    let dateOfBirth: Date
    ...
    }
    let person = Person(name: "Ash", dateOfBirth: ...

    View full-size slide

  32. Structs vs Classes
    —Pass by value vs. by reference.
    —Classes can extend other classes for OOP.
    —Structs have no hierarchy, cannot extend other
    structs.

    View full-size slide

  33. Extensions
    Extensions are the ability to add functions to
    existing types.
    extension Int {
    func isNegative() -> Bool {
    return self < 0
    }
    }
    1.isNegative() // returns false
    (-1).isNegative() // returns true

    View full-size slide

  34. FUNctions
    func isMultipleOfTwo(number: Int) -> Bool {
    return (number % 2 == 0)
    }
    [1, 2, 3, 4, 5].filter(isMultipleOfTwo)

    View full-size slide

  35. Function Currying
    func isMultipleOf(_ multiple: Int) -> (Int) -> Bool {
    return { number in
    return (number % multiple == 0)
    }
    }
    [1, 2, 3, 4, 5].filter(isMultipleOf(2)) // Returns [2, 4]

    View full-size slide

  36. Protocols
    Protocols are for loosely-coupled contracts. In other
    languages, they're sometimes called interfaces.
    They are a way to know that a type has some
    functions without knowing what that type is.
    Example: Table view data source.

    View full-size slide

  37. Extending for Protocol Conformance
    Extensions can add protocol conformance to
    existing types, too.
    protocol MyProtocol {
    func doSomething()
    }
    extension Int: MyProtocol {
    func doSomething() {
    ...
    }
    }

    View full-size slide

  38. Optionals
    Optionals are a way to define whether or not a value
    can be nil, or "missing". Optionals are signified
    by ? and are a type. So Int? is an optional integer
    because it might be nil.
    This is another improvement over Objective-C.

    View full-size slide

  39. Generics
    Generic types are types that hold other data – any
    data. For example, Array is a generic because it
    can hold any type. Let's make a queue.

    View full-size slide

  40. Generics
    struct Queue {
    private var array = [T]()
    func push(element: T) {
    array.append(element)
    }
    func pop() -> T? {
    return array.remove(at: 0)
    }
    }

    View full-size slide

  41. Extensions
    Extensions add behaviour to types.
    extension Int {
    var isEven: Bool {
    return (self % 2 == 0)
    }
    }

    View full-size slide

  42. Extensions
    protocol Occupiable {
    var isEmpty: Bool
    var isNotEmpty: Bool
    }
    extension String: Occupiable {
    var isEmpty: Bool {
    return self.characters.count > 0
    }
    var isNotEmpty: Bool {
    return !self.isEmpty
    }
    }

    View full-size slide

  43. Extensions
    protocol Occupiable {
    var isEmpty: Bool
    var isNotEmpty: Bool
    }
    extension Occupiable {
    var isNotEmpty: Bool {
    return !self.isEmpty
    }
    }
    extension String: Occupiable {}

    View full-size slide

  44. Extensions with Generics
    We can extend generic types with where clauses so
    the extension only exists when that clause is true.
    For example, if we want to extend arrays of things
    that conform to Occupiable:
    extension Array where Element: Occupiable {
    var isFilledWithEmpties: Bool {
    return self.filter({ $0.isNotEmpty }).count > 0
    }
    }

    View full-size slide

  45. Magic
    [1, 2, 3, 4, 5].map(isMultipleOf(2)) // [false, true, false, true, false]
    [1, 2, 3, 4, 5].reduce(0, +) // Adds numbers together
    [1, 2, 3, 4, 5].reduce(Int.min, >) // Finds largest number

    View full-size slide

  46. Wrap-up
    1. Swift was a strategic replacement for Objective-C
    2. Open sourcing Swift has been good for the
    community
    3. Swift syntax supports a variety of paradigms

    View full-size slide

  47. Next steps
    —Swift Playgrounds
    —Swift on Linux
    —docker-swift
    —Xcode

    View full-size slide