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 Slide

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

    View Slide

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

    View Slide

  4. History

    View Slide

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

    View Slide

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

    View Slide

  7. Replacing Objective-C was a
    strategic necessity

    View Slide

  8. View Slide

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

    View Slide

  10. Swift expectations

    View Slide

  11. Open Source
    Swift

    View Slide

  12. Swift Evolution

    View Slide

  13. bugs.swift.org

    View Slide

  14. Swift on
    Linux

    View Slide

  15. Swift is a multi-purpose language

    View Slide

  16. Swift
    Syntax

    View Slide

  17. Swift is
    multi-platform
    and
    multi-paradigm

    View Slide

  18. 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  25. 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 Slide

  26. 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 Slide

  27. 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 Slide

  28. 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

  32. 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 Slide

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

    View Slide

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

    View Slide

  35. 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 Slide

  36. 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 Slide

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

    View Slide

  38. 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 Slide

  39. 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 Slide

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

    View Slide

  41. 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 Slide

  42. 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 Slide

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

    View Slide

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

    View Slide

  45. 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 Slide

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

    View Slide

  47. 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 Slide

  48. 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 Slide

  49. 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 Slide

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

    View Slide

  51. Go explore

    View Slide