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

Introduction to Swift Keypaths

Introduction to Swift Keypaths

Keypaths were added in Swift 4. They're a fantastic feature but sometimes it feels difficult to find good situations for using them. However, when understood and used the right way, keypaths allow you to implement additional abstractions into your codebase that would be much harder with generics or protocols. In order to be able to do that, though, it is useful to intuitively understand when to apply the Keypath pattern.

This talk will first explain the different types of keypaths that exist and then go through real world examples to showcase how they can be leveraged in your own projects.

Benedikt Terhechte

March 21, 2019
Tweet

More Decks by Benedikt Terhechte

Other Decks in Programming

Transcript

  1. @terhechte お早う! 2 # Benedikt Terhechte # Twitter: @terhechte #

    Swift Guides: appventure.me # Swift Podcast: contravariance.rocks
  2. @terhechte What are KeyPaths # Swift 4 # Type-Safe shortcuts

    to Read / Write properties # Composable 3
  3. struct ProfileSettings { var displayName: String var shareUpdates: Bool var

    score: Float } struct PrivacySettings { var passcode: Bool var addByID: Bool var blackList: [String] }
  4. Abstract Over Types struct ProfileSettings { var displayName: String var

    shareUpdates: Bool var score: Float } struct PrivacySettings { var passcode: Bool var addByID: Bool var blackList: [String] } String, Bool, Float Bool, Bool, [String]
  5. @terhechte KeyPaths # Protocols make it difficult to abstract over

    very different types # KeyPaths allow to do this 9
  6. @terhechte Goal # Develop generic app settings # Settings of

    any shape / type # To achieve that, we will learn about KeyPaths 10
  7. @terhechte Agenda # Intro # KeyPath Theory # Practical Example

    # Tips And Tricks # KeyPath Libraries 11
  8. Abstract the access to the property "username" into a variable

    that can be moved and stored let nameKeyPath = \User.username
  9. Nesting: struct Address { let street: String } struct User

    { let address: Address } let x = \User.address.street.count KeyPath<User, Int>
  10. KeyPath<Root, Value> struct User { let username: String } let

    kp: KeyPath<User, String> = \User.username let player = User(username: “Mario”) print(player[keyPath: kp]) // Error. Read Only KeyPath player[keyPath: kp] = “Luigi"
  11. WritableKeyPath<Type, Value> struct User { var username: String } var

    player = User(username: “Mario”) player[keyPath: \User.username] = “Luigi”
  12. @terhechte ReferenceWritableKeyPath<Root, Value> # Read / Write KeyPath for Class

    types # Useful for mutating properties of let roots 25
  13. PartialKeyPath<Root> // String let a: PartialKeyPath<User> = \User.name // Float

    let c: PartialKeyPath<User> = \User.quote // Int let b: PartialKeyPath<User> = \User.age
  14. @terhechte AnyKeyPath # No Root, no Value # Type-Erased KeyPath.

    # Very useful to keep different types together 29
  15. @terhechte You can cast types back # AnyKeyPath as? WritableKeyPath<User,

    String> # PartialKeyPath<User> as? KeyPath<User, Bool> 31
  16. KeyPath Composition // User -> address let addressKeyPath = \User.address

    // Address -> street let streetKeyPath = \Address.street // User -> address -> street let userStreetKeyPath = addressKeyPath .appending(path: streetKeyPath)
  17. struct Settings { var profileSettings: ProfileSettings var privacySettings: PrivacySettings }

    struct ProfileSettings { var displayName: String var shareUpdates: Bool }
  18. struct Settings { var profileSettings: ProfileSettings var privacySettings: PrivacySettings }

    struct ProfileSettings { var displayName: String var shareUpdates: Bool } struct PrivacySettings { var passcode: Bool var addByID: Bool }
  19. Settings Entry Struct struct SettingsEntry { let keyPath: AnyKeyPath let

    title: String let subtitle: String let help: String ... }
  20. Settings Entry Struct struct SettingsEntry { let keyPath: AnyKeyPath let

    title: String <— “Lock with Passcode” let subtitle: String let help: String ... }
  21. extension Settings: SettingsProvider { var settings: [SettingsEntry] { return [

    SettingsEntry( keyPath: \Settings.profileSettings, title: "Profile"), SettingsEntry( keyPath: \Settings.privacySettings, title: "Privacy") ] } }
  22. extension ProfileSettings: SettingsProvider { var settings: [SettingsEntry] { return [

    SettingsEntry( keyPath: \ProfileSettings.displayName, title: "Display Name"), SettingsEntry( keyPath: \ProfileSettings.shareUpdates, title: "Share Profile Media Updates") ] } }
  23. extension PrivacySettings: SettingsProvider { var settings: [SettingsEntry] { return [

    SettingsEntry( keyPath: \PrivacySettings.addByID, title: "Allow add me by ID"), SettingsEntry( keyPath: \PrivacySettings.passcode, title: "Passcode Lock") ] } }
  24. func editSettings(provider: inout SettingsProvider) { for setting in provider.settings {

    let value = provider[keyPath: setting.keyPath] if let nested = value as? SettingsProvider { ... } else { ... } } }
  25. func editSettings(provider: inout SettingsProvider) { for setting in provider.settings {

    let value = provider[keyPath: setting.keyPath] if let nested = value as? SettingsProvider { for item in nested.settings {
  26. func editSettings(provider: inout SettingsProvider) { for setting in provider.settings {

    let value = provider[keyPath: setting.keyPath] if let nested = value as? SettingsProvider { for item in nested.settings { if let joined = keyPath.appending(path: item.keyPath) {
  27. func editSettings(provider: inout SettingsProvider) { for setting in provider.settings {

    let value = provider[keyPath: setting.keyPath] if let nested = value as? SettingsProvider { for item in nested.settings { if let joined = keyPath.appending(path: item.keyPath) { \Settings.PrivacySettings
  28. func editSettings(provider: inout SettingsProvider) { for setting in provider.settings {

    let value = provider[keyPath: setting.keyPath] if let nested = value as? SettingsProvider { for item in nested.settings { if let joined = keyPath.appending(path: item.keyPath) { \PrivacySettings.passcode
  29. func editSettings(provider: inout SettingsProvider) { for setting in provider.settings {

    let value = provider[keyPath: keyPath] if let nested = value as? SettingsProvider { for item in nested.settings { if let joined = keyPath.appending(path: item.keyPath) { updateSetting(keyPath: joined, title: item.title) ... }
  30. func editSettings(provider: inout SettingsProvider) { func updateSetting(keyPath: AnyKeyPath, title: String)

    { let value = provider[keyPath: setting.keyPath] if let nested = value as? SettingsProvider { for item in nested.settings { if let joined = keyPath.appending(path: item.keyPath) { updateSetting(keyPath: joined, title: item.title) ... }
  31. ☑ Settings: SettingsProvider { ☑ profileSettings: SettingsProvider { displayName: String,

    shareUpdates: Bool }, ☑ privacySettings: SettingsProvider { }, }
  32. if let nested = value as? SettingsProvider { for item

    in nested.settings { if let joined = keyPath.appending(path: item.keyPath) { updateSetting(keyPath: joined, title: item.title) } } } else { } }
  33. if let nested = value as? SettingsProvider { for item

    in nested.settings { if let joined = keyPath.appending(path: item.keyPath) { updateSetting(keyPath: joined, title: item.title) } } } else { if let writable = keyPath as? WritableKeyPath<Root, Bool> { provider[keyPath: writable] = true } }
  34. if let nested = value as? SettingsProvider { for item

    in nested.settings { if let joined = keyPath.appending(path: item.keyPath) { updateSetting(keyPath: joined, title: item.title) } } } else { if let writable = keyPath as? WritableKeyPath<Root, Bool> { titleLabel.text = setting.title provider[keyPath: writable] = true } }
  35. func editSettings<Root: SettingsProvider>(provider: inout Root) { func updateSetting(keyPath: AnyKeyPath, title:

    String) { let value = provider[keyPath: keyPath] if let nestedProvider = value as? SettingsProvider { for item in nestedProvider.settings { if let joined = keyPath.appending(path: item.keyPath) { updateSetting(keyPath: joined, title: item.title) } } } else if let writable = keyPath as? WritableKeyPath<Root, Bool> { provider[keyPath: writable] = true } } for setting in provider.settings { updateSetting(keyPath: setting.keyPath, title: setting.title) } }
  36. @terhechte Abstract over properties # Types of any shape can

    now be combined into our settings # Can have generic UI elements to read / write the types # They just need to describe themselves via [SettingEntry] 81
  37. @terhechte 1. Choose which Types to Erase # KeyPath<A, B>

    = \User.age # PartialKeyPath<A> = \User.age # AnyKeyPath = \User.age 83
  38. @terhechte 2. You can cast types back # AnyKeyPath as?

    WritableKeyPath<User, String> # PartialKeyPath<User> as? KeyPath<User, Bool> 84
  39. @terhechte 3. KeyPaths conform to Hashable # Can be Keys

    in dictionaries # Useful to store more information about Keys 85
  40. let meta: [PartialKeyPath<User>: String] = [ \User.username: “Your Name”, \User.age:

    “Your Age” ] func renderTitle(keyPath: AnyKeyPath) { } renderTitle(\User.username)
  41. let meta: [PartialKeyPath<User>: String] = [ \User.username: “Your Name”, \User.age:

    “Your Age” ] func renderTitle(keyPath: AnyKeyPath) { if let title = meta[keyPath] titleField.text = title } } renderTitle(\User.username)
  42. Kuery Example // Before: NSPredicate(format: "name == %@", “Mario") NSPredicate(format:

    "age > %@", 20) // After: Query(Person.self).filter(\Person.name == “Mario") Query(Person.self).filter(\Person.age > 20) github.com/kishikawakatsumi/Kuery
  43. KeyPathKit contacts.filter(where: \.lastName == “Webb" && \.age < 40) contacts.average(of:

    \.age).rounded() contacts.between(\.age, range: 20...30) contacts.groupBy(\.lastName) github.com/vincent-pradeilles/KeyPathKit
  44. @terhechte What did we learn # KeyPaths are type-safe, type-erased,

    hashable and composable # Create abstractions not possible with protocols 99
  45. @terhechte More Information # https://www.swiftbysundell.com/posts/the-power-of-key-paths-in-swift # https://www.klundberg.com/blog/swift-4-keypaths-and-you/ # https://github.com/vincent-pradeilles/slides/blob/master/iosconfsg-2019-the-underestimated- power-of-keypaths.pdf

    # https://blog.slashkeys.com/practical-keypaths-in-swift-220da5ab5950 # https://edit.theappbusiness.com/using-swift-keypaths-for-beautiful-user-preferences-c83c2f7ea7be # https://www.swiftbysundell.com/posts/the-power-of-key-paths-in-swift # https://github.com/makskovalko/FormValidation 100
  46. • German Social Network • Based in Hamburg • 15

    Mio Users • Native Apps on all platforms We’re Hiring