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

Evolucionando con Swift

Evolucionando con Swift

En la primera charla, @marcosgriselli nos va a contar acerca de algunas features nuevas de Swift 4.1 y 4.2. Analizaremos cómo estos cambios nos llevarán a escribir código más seguro y expresivo, y cómo podemos aplicarlo en el día a día de nuestras apps. También, repasaremos alguna proposal de Swift Evolution ya aceptada y veremos cómo esto impactará a futuro en nuestro código.

Marcos Griselli

August 16, 2018
Tweet

Other Decks in Programming

Transcript

  1. Evolucionando con Swift
    Como escribir código más expresivo y
    seguro utilizando las nuevas
    funcionalidades del lenguaje.
    Marcos Griselli
    iOS Developer @ Toptal
    Marcos Griselli - SwiftBA 1

    View Slide

  2. Evolución
    Marcos Griselli - SwiftBA 2

    View Slide

  3. Swift 1.2
    » flatMap
    » @noescape
    » Set
    » Multiple if-let
    Marcos Griselli - SwiftBA 3

    View Slide

  4. Swift 2
    » try/catch
    » guard
    » defer
    » API availability
    Marcos Griselli - SwiftBA 4

    View Slide

  5. Swift 2.1
    » String interpolation
    "String \(value)"
    Swift 2.2
    Primer release desde que es open source
    » C style deprecations (++, --, loops)
    » var parameters deprecation
    Marcos Griselli - SwiftBA 5

    View Slide

  6. Swift 3
    » Better Translation of Objective-C APIs
    Into Swift
    » Apply API Guidelines to the Standard
    Library
    » ~100 proposals
    Marcos Griselli - SwiftBA 6

    View Slide

  7. Swift 3.1
    » Concrete Constrained Extensions
    » Nested Generics
    Marcos Griselli - SwiftBA 7

    View Slide

  8. Swift 4
    » Codable
    » String revision (SE-0163)
    » Multi-line string literals
    » Improve keypaths and Dictionary
    Marcos Griselli - SwiftBA 8

    View Slide

  9. Swift 4.1
    » Synthesized Equatable and Hashable
    (SE-0185)
    » Conditional conformances (SE-0143)
    » Key decoding strategy for Codable
    » Recursive constraints on associated
    types
    » Introduce Sequence.compactMap(_:)
    Marcos Griselli - SwiftBA 9

    View Slide

  10. Hashable & Equatable
    Marcos Griselli - SwiftBA 10

    View Slide

  11. Swift 4.0
    struct User: Decodable {
    let id: String
    let name: String
    let email: String
    let registerDate: Date
    let talks: [Talk]
    }
    Marcos Griselli - SwiftBA 11

    View Slide

  12. // MARK: - Hashable
    extension User: Hashable {
    var hashValue: Int {
    return combineHashes([id.hashValue,
    name.hashValue,
    email.hashValue,
    registerDate.hashValue,
    talks.hashValue])
    }
    }
    Marcos Griselli - SwiftBA 12

    View Slide

  13. // MARK: - Equatable
    extension User: Equatable {
    static func == (lhs: User, rhs: User) -> Bool {
    return lhs.id == rhs.id &&
    lhs.name == rhs.name &&
    lhs.email == rhs.email &&
    lhs.registerDate == rhs.registerDate &&
    lhs.talks == rhs.talks
    }
    }
    Marcos Griselli - SwiftBA 13

    View Slide

  14. Hashable & Equatable en
    Swift 4.0
    » Implementación duplicada
    » Propenso a errores al modificar
    nuestras estructuras
    » Complejo de agregar en ciertos casos
    (enum con associated values)
    Marcos Griselli - SwiftBA 14

    View Slide

  15. SE-0185
    Synthesized Equatable and Hashable
    Marcos Griselli - SwiftBA 15

    View Slide

  16. Swift 4.1
    struct User: Decodable, Hashable, Equatable {
    let id: String
    let name: String
    let registerDate: Date
    let talks: [Talk]
    }
    » Eliminar boilerplate y errores del
    programador.
    » Conformar a Hashable o Equatable de
    manera simple.
    Marcos Griselli - SwiftBA 16

    View Slide

  17. SE-0143
    Conditional conformances
    Marcos Griselli - SwiftBA 17

    View Slide

  18. “The ability for
    types that store
    other types to
    conform to a
    protocol”
    https://swift.org/blog/conditional-
    conformance/
    Marcos Griselli - SwiftBA 18

    View Slide

  19. Esto puede verse reflejado en Arrays u
    Opcionales
    extension Array: Equatable where Element: Equatable {
    // implementation of == for Array
    }
    extension Optional: Equatable where Wrapped: Equatable {
    // implementation of == for Optional
    }
    /// [Int?]
    let a = [Int("1"), Int("2")]
    let b = [Int("1"), Int("2")]
    a == b // Swift 4.0

    a == b // Swift 4.1

    Marcos Griselli - SwiftBA 19

    View Slide

  20. Como podemos utilizarlo?
    Marcos Griselli - SwiftBA 20

    View Slide

  21. struct SomeWrapper {
    let wrapped: Wrapped
    }
    protocol HasIdentity {
    static func ===(lhs: Self, rhs: Self) -> Bool
    }
    extension SomeWrapper: Equatable where Wrapped: Equatable {
    static func ==(lhs: SomeWrapper, rhs: SomeWrapper) -> Bool {
    return lhs.wrapped == rhs.wrapped
    }
    }
    // error: SomeWrapper already stated conformance to Equatable
    extension SomeWrapper: Equatable where Wrapped: HasIdentity {
    static func ==(lhs: SomeWrapper, rhs: SomeWrapper) -> Bool {
    return lhs.wrapped === rhs.wrapped
    }
    }
    Marcos Griselli - SwiftBA 21

    View Slide

  22. » Podemos crear wrappers alrededor de
    cualquier tipo y aún conformar a los
    protocolos que necesitemos.
    » Con ayuda de Phantom Types podemos
    generar tipos que sean únicos aunque el
    tipo que contengan sea el mismo.
    Marcos Griselli - SwiftBA 22

    View Slide

  23. Tagged
    by PointFree
    struct Tagged {
    var rawValue: RawValue
    }
    Marcos Griselli - SwiftBA 23

    View Slide

  24. Conditional
    conformances
    // MARK: - Equatable
    extension Tagged where RawValue: Equatable {
    static func == (lhs: Tagged, rhs: Tagged) -> Bool {
    return lhs.rawValue == rhs.rawValue
    }
    }
    // MARK: - Decodable
    extension Tagged: Decodable where RawValue: Decodable {
    init(from decoder: Decoder) throws {
    self.init(rawValue: try decoder.singleValueContainer()
    .decode(RawValue.self))
    }
    }
    Marcos Griselli - SwiftBA 24

    View Slide

  25. Xcode!
    Marcos Griselli - SwiftBA 25

    View Slide

  26. Swift 4.2
    » Enhanced Hashable
    » Dynamic Member Lookup (SE-0195)
    » Case Iterable (SE-0194)
    » Random API
    » Warnings and Errors (SE-0196)
    Marcos Griselli - SwiftBA 26

    View Slide