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

What's new in Swift 4.2 and Xcode 10

What's new in Swift 4.2 and Xcode 10

In this talk I will focus on the improvements and additions in the newly released Swift 4.2 alongside with changes in Xcode 10 (dark mode!!).

Dominik Hádl

October 05, 2018
Tweet

More Decks by Dominik Hádl

Other Decks in Programming

Transcript

  1. We deliver complex enterprise-level apps without compromising on outstanding user

    experience and eye-catching design. – Copenhagen, Aarhus, London, Manchester, Amsterdam, Berlin and Prague – Experts in full-service mobile applications development – Over 1100 applications and other digital solutions – 90+ team members across various teams (UX, UI, iOS, Android, Vapor…) “Enterprise apps made awesome”
  2. Overview - Dynamic Member Lookup - New API for Random

    & Shuffling - Enumeration Improvements - Sequence Methods Consistency & Testing Elements - Conditional Conformance Updates - Hashing Made Easy - Lots of small fixes...
  3. Dynamic Member Lookup Before class Car { let brand: String

    let year: Int private let features: [String: String] init(brand: String, year: Int, features: [String: String]) { self.brand = brand self.year = year self.features = features } subscript(key: String) -> String { switch key { case "info": return "\(brand) was made in year \(year)." default: return features[key] ?? "" } } }
  4. Dynamic Member Lookup Before class Car { let brand: String

    let year: Int private let features: [String: String] init(brand: String, year: Int, features: [String: String]) { self.brand = brand self.year = year self.features = features } subscript(key: String) -> String { switch key { case "info": return "\(brand) was made in year \(year)." default: return features[key] ?? "" } } } let car = Car( brand: “Skoda”, year: 2002, features: [ “transmission” : ”automatic” ] ) print(car[“info”]) print(car[“transmission”])
  5. Dynamic Member Lookup Before class Car { let brand: String

    let year: Int private let features: [String: String] init(brand: String, year: Int, features: [String: String]) { self.brand = brand self.year = year self.features = features } subscript(key: String) -> String { switch key { case "info": return "\(brand) was made in year \(year)." default: return features[key] ?? "" } } } let car = Car( brand: “Skoda”, year: 2002, features: [ “transmission” : ”automatic” ] ) print(car[“info”]) print(car[“transmission”])
  6. Dynamic Member Lookup Now class Car { let brand: String

    let year: Int private let features: [String: String] init(brand: String, year: Int, features: [String: String]) { self.brand = brand self.year = year self.features = features } subscript(key: String) -> String { switch key { case "info": return "\(brand) was made in year \(year)." default: return features[key] ?? "" } } }
  7. Dynamic Member Lookup Now @dynamicMemberLookup class Car { let brand:

    String let year: Int private let features: [String: String] init(brand: String, year: Int, features: [String: String]) { self.brand = brand self.year = year self.features = features } subscript( dynamicMember key: String) -> String { switch key { case "info": return "\(brand) was made in year \(year)." default: return features[key] ?? "" } } }
  8. Dynamic Member Lookup Now @dynamicMemberLookup class Car { let brand:

    String let year: Int private let features: [String: String] init(brand: String, year: Int, features: [String: String]) { self.brand = brand self.year = year self.features = features } subscript( dynamicMember key: String) -> String { switch key { case "info": return "\(brand) was made in year \(year)." default: return features[key] ?? "" } } } let car = Car( brand: “Skoda”, year: 2002, features: [ “transmission” : ”automatic” ] ) print(car.info) print(car.transmission)
  9. Dynamic Member Lookup Now @dynamicMemberLookup class Car { let brand:

    String let year: Int private let features: [String: String] init(brand: String, year: Int, features: [String: String]) { self.brand = brand self.year = year self.features = features } subscript( dynamicMember key: String) -> String { switch key { case "info": return "\(brand) was made in year \(year)." default: return features[key] ?? "" } } } let car = Car( brand: “Skoda”, year: 2002, features: [ “transmission” : ”automatic” ] ) print(car.info) print(car.transmission)
  10. New API for Random & Shuffling Now let number =

    Int.random(in: 0..<10) Before let number = Int(arc4random_unfiform(10))
  11. New API for Random & Shuffling Before let meetupMembers =

    [“Dominik”, “Denis”, “Pavel”, “Petr”] let index = Int(arc4random_uniform(UInt32(meetupMembers.count))) let member = meetupMembers[index]
  12. New API for Random & Shuffling Before let meetupMembers =

    [“Dominik”, “Denis”, “Pavel”, “Petr”] let index = Int(arc4random_uniform(UInt32(meetupMembers.count))) let member = meetupMembers[index]
  13. New API for Random & Shuffling Before let meetupMembers =

    [“Dominik”, “Denis”, “Pavel”, “Petr”] let index = Int(arc4random_uniform(UInt32(meetupMembers.count))) let member = meetupMembers[index] Now let meetupMembers = [“Dominik”, “Denis”, “Pavel”, “Petr”] meetupMembers.randomElement()
  14. New API for Random & Shuffling Before let meetupMembers =

    [“Dominik”, “Pavel”, “Petr”] meetupMemebers.sort { _, _ in arc4random_uniform(2) == 0 }
  15. New API for Random & Shuffling Before let meetupMembers =

    [“Dominik”, “Pavel”, “Petr”] meetupMemebers.sort { _, _ in arc4random_uniform(2) == 0 }
  16. New API for Random & Shuffling Now let meetupMembers =

    [“Dominik”, “Pavel”, “Petr”] meetupMembers.shuffle() Before let meetupMembers = [“Dominik”, “Pavel”, “Petr”] meetupMemebers.sort { _, _ in arc4random_uniform(2) == 0 }
  17. Enumeration Improvements Before enum MeeetupType { case iOS, android let

    allCases: [MeetupType] = [.iOS, .android] } Now enum MeeetupType: CaseIterable { case iOS, android }
  18. Sequence Methods Consistency & Testing Elements Before let members =

    [“Dominik”, “Denis”, “Pavel”, “Petr”, “Dominik”] let firstMember = members.first(where: { $0.hasPrefix(“P”) }) let firstIndex = members.index(where: { $0.hasPrefix(“P”) }) let firstMemberIndex = members.index(of: “Dominik”)
  19. Sequence Methods Consistency & Testing Elements Before let members =

    [“Dominik”, “Denis”, “Pavel”, “Petr”, “Dominik”] let firstMember = members.first(where: { $0.hasPrefix(“P”) }) let firstIndex = members.index(where: { $0.hasPrefix(“P”) }) let firstMemberIndex = members.index(of: “Dominik”)
  20. Sequence Methods Consistency & Testing Elements Now let members =

    [“Dominik”, “Denis”, “Pavel”, “Petr”, “Dominik”] let firstMember = members.first(where: { $0.hasPrefix(“P”) }) let firstIndex = members. firstIndex(where: { $0.hasPrefix(“P”) }) let firstMemberIndex = members. firstIndex(of: “Dominik”) let lastMember = members. last(where: { $0.hasPrefix(“P”) }) let lastIndex = members. lastIndex(where: { $0.hasPrefix(“P”) }) let lastMemberIndex = members. lastIndex(of: “Dominik”) Before let members = [“Dominik”, “Denis”, “Pavel”, “Petr”, “Dominik”] let firstMember = members.first(where: { $0.hasPrefix(“P”) }) let firstIndex = members.index(where: { $0.hasPrefix(“P”) }) let firstMemberIndex = members.index(of: “Dominik”)
  21. Sequence Methods Consistency & Testing Elements Before let numbers =

    [20, 3, 54, 12] let containsOnlyEvenNumbers = !numbers.contains { $0 % 2 == 1 }
  22. Sequence Methods Consistency & Testing Elements Before let numbers =

    [20, 3, 54, 12] let containsOnlyEvenNumbers = !numbers.contains { $0 % 2 == 1 }
  23. Sequence Methods Consistency & Testing Elements Now let numbers =

    [20, 3, 54, 12] let containsOnlyEvenNumbers = numbers.allSatisfy { $0 % 2 == 1 } Before let numbers = [20, 3, 54, 12] let containsOnlyEvenNumbers = !numbers.contains { $0 % 2 == 1 }
  24. Conditional Conformance Updates Conditional conformance in extensions (!!!) extension Array:

    Equatable where Element: Equatable { static func ==(lhs: Array<Element>, rhs: Array<Element>) -> Bool { ... } }
  25. Conditional Conformance Updates Querying conformance at runtime if let array

    = data as? Equatable { print(“Array is equatable”) } Conditional conformance in extensions (!!!) extension Array: Equatable where Element: Equatable { static func ==(lhs: Array<Element>, rhs: Array<Element>) -> Bool { ... } }
  26. Conditional Conformance Updates Querying conformance at runtime if let array

    = data as? Equatable { print(“Array is equatable”) } Conditional conformance in extensions (!!!) extension Array: Equatable where Element: Equatable { static func ==(lhs: Array<Element>, rhs: Array<Element>) -> Bool { ... } } Hashable conditional conformance improvements Optionals, arrays, dictionaries and ranges are now Hashable whenever their members are Hashable as well.
  27. Hashing Made Easy Before class MyClass: Hashable { let someProperty:

    String = “some value” var hashValue: Int { return someProperty.hashValue &* 16777619 } }
  28. Hashing Made Easy Before class MyClass: Hashable { let someProperty:

    String = “some value” var hashValue: Int { return someProperty.hashValue &* 16777619 } }
  29. Hashing Made Easy Now class MyClass: Hashable { let someProperty:

    String = “some value” func hash(into hasher: inout Hasher) { hasher.combine(someProperty) } } Before class MyClass: Hashable { let someProperty: String = “some value” var hashValue: Int { return someProperty.hashValue &* 16777619 } }
  30. Other Improvements Removing collection elements array.removeAll { $0.isValid } Toggling

    bool’s var isOn = true isOn.toggle() New compiler directives #warning(“This should be removed on release”) #error(“Insert API key”)
  31. The most loved and hated feature of Swift 4.2 let

    completion: (() -> Void) = { [weak self] in guard let self = self else { return } print(self) // not an optional anymore }
  32. Summary - Dynamic Member Lookup - New API for Random

    & Shuffling - Enumeration Improvements - Sequence Methods Consistency & Testing Elements - Conditional Conformance Updates - Hashing Made Easy - Lots of small fixes...
  33. List of new features - Interface Builder improvements (library window,

    font preview..) - Multi cursor editing - Custom code snippets
  34. List of new features - Interface Builder improvements (library window,

    font preview..) - Multi cursor editing - Custom code snippets - New build system
  35. List of new features - Interface Builder improvements (library window,

    font preview..) - Multi cursor editing - Custom code snippets - New build system - Schemes shared by default
  36. List of new features - Interface Builder improvements (library window,

    font preview..) - Multi cursor editing - Custom code snippets - New build system - Schemes shared by default - Testing in parallel
  37. List of new features - Interface Builder improvements (library window,

    font preview..) - Multi cursor editing - Custom code snippets - New build system - Schemes shared by default - Testing in parallel - Source control improvements