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. Requirements for an Objective-C replacement 1. No pointers 2. Managed

    memory 3. Native unicode support 4. Concise 5. Named parameters
  2. 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.
  3. Built-in Types —String (ex: "Hello, world!") —Int (ex: 1234 or

    -1) —Bool (ex: true or false) —etc...
  4. Enums An enum is a type with mutually-exclusive cases. enum

    ArtworkAvailability { case notForSale case forSale case onHold case sold } ArtworkAvailability.forSale
  5. Emoji Swift has full unicode support, so you can make

    enum cases have emoji, if you want to. enum Animals { case ! case " case # }
  6. Arrays Arrays are variable-length data containers for homogenous data. ["Orta",

    "Ash", "Eloy", "Sarah", "Maxim"] [1, 200, -5, 73] [ArtworkAvailability.forSale, .onHold, .sold]
  7. 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.
  8. 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.
  9. Tuples Tuples are fixed-length data containers for heterogenous data. let

    presenter = ("Ash", [Animal.!, .!]) presenter.0 // "Ash" presenter.1 // [Animal.!, .!] presenter.2 // compiler error!
  10. 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.!, .!]
  11. Functions Functions are pieces of code. They can optionally accept

    input and/or return output. func sayHi() { print("Hello, world!") } sayHi()
  12. Functions func greet(name: String) -> String { return "Hello, "

    + name } print(greet(name: "Ash")) // prints "Hello, Ash"
  13. 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 }
  14. Classes Classes contain data and are passed by reference. class

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

    Person { let name: String let dateOfBirth: Date ... } let person = Person(name: "Ash", dateOfBirth: ...
  16. Structs vs Classes —Pass by value vs. by reference. —Classes

    can extend other classes for OOP. —Structs have no hierarchy, cannot extend other structs.
  17. 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
  18. FUNctions func isMultipleOfTwo(number: Int) -> Bool { return (number %

    2 == 0) } [1, 2, 3, 4, 5].filter(isMultipleOfTwo)
  19. 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]
  20. 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.
  21. Extending for Protocol Conformance Extensions can add protocol conformance to

    existing types, too. protocol MyProtocol { func doSomething() } extension Int: MyProtocol { func doSomething() { ... } }
  22. 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.
  23. 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.
  24. Generics struct Queue<T> { private var array = [T]() func

    push(element: T) { array.append(element) } func pop() -> T? { return array.remove(at: 0) } }
  25. Extensions Extensions add behaviour to types. extension Int { var

    isEven: Bool { return (self % 2 == 0) } }
  26. 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 } }
  27. Extensions protocol Occupiable { var isEmpty: Bool var isNotEmpty: Bool

    } extension Occupiable { var isNotEmpty: Bool { return !self.isEmpty } } extension String: Occupiable {}
  28. 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 } }
  29. 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
  30. 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