Swift, Briskly

0ebf471a3ae8df42a84f93a7efbbdbd0?s=47 Ash Furrow
November 03, 2016

Swift, Briskly

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

0ebf471a3ae8df42a84f93a7efbbdbd0?s=128

Ash Furrow

November 03, 2016
Tweet

Transcript

  1. Swift, Briskly Ash Furrow, Artsy

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

    job interview
  3. Agenda 1. The history of Swift 2. How Swift became

    open source 3. Swift syntax
  4. History

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

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

  7. Replacing Objective-C was a strategic necessity

  8. None
  9. Requirements for an Objective-C replacement 1. No pointers 2. Managed

    memory 3. Native unicode support 4. Concise 5. Named parameters
  10. Swift expectations

  11. Open Source Swift

  12. Swift Evolution

  13. bugs.swift.org

  14. Swift on Linux

  15. Swift is a multi-purpose language

  16. Swift Syntax

  17. Swift is multi-platform and multi-paradigm

  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.
  19. Types Example types: —Integers —Booleans —Functions —Classes Swift is strongly-typed.

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

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

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

    enum cases have emoji, if you want to. enum Animals { case ! case " case # }
  23. Enums Enums are super-powerful. Here’re two examples: enum Bool {

    case true case false } enum Never { }
  24. Arrays Arrays are variable-length data containers for homogenous data. ["Orta",

    "Ash", "Eloy", "Sarah", "Maxim"] [1, 200, -5, 73] [ArtworkAvailability.forSale, .onHold, .sold]
  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.
  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.
  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!
  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.!, .!]
  29. Functions Functions are pieces of code. They can optionally accept

    input and/or return output. func sayHi() { print("Hello, world!") } sayHi()
  30. Functions func say(message: String) { print(message) } say(message: "Hello, world!")

  31. Functions func greet(name: String) -> String { return "Hello, "

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

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

    Person { let name: String let dateOfBirth: Date ... } let person = Person(name: "Ash", dateOfBirth: ...
  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.
  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
  37. FUNctions func isMultipleOfTwo(number: Int) -> Bool { return (number %

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

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

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

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

    } extension Occupiable { var isNotEmpty: Bool { return !self.isEmpty } } extension String: Occupiable {}
  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 } }
  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
  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
  50. Next steps —Swift Playgrounds —Swift on Linux —docker-swift —Xcode

  51. Go explore