Types of KeyPath in Swift.

sergeyleschev

Sergey Leschev

Posted on January 29, 2023

Types of KeyPath in Swift.

Swift has five key path types, but we can categorize them into two groups based on their functions.

  1. Read-only key paths.
  2. Writable key paths (Can read and write).

We have three read-only key paths.

  1. KeyPath
  2. ParialKeyPath
  3. AnyKeyPath

And two writable key paths.

I will only focus on three basic types of key paths in this article.

1 KeyPath: A read-only access to a property. Root type can be both value/reference semantics.
2 WritableKeyPath: Provides read-write access to a mutable property with value semantics (such as struct and enum).
3 ReferenceWritableKeyPath: Provides reading and writing to a mutable property with reference semantics (such as class).

How can key path type inferred

In the last section, when we construct key paths, you can see that we only get KeyPath and WritableKeyPath.

let firstIndexInteger = \[Int][0]
// WritableKeyPath<[Int], Int>

let firstInteger = \Array<Int>.first
// KeyPath<[Int], Int?>
Enter fullscreen mode Exit fullscreen mode

Key path infers from the properties/subscripts and a root type.

  • If properties or subscripts are read-only (such as let or subscript with only get), KeyPath is inferred.

  • If it is mutable (such as var or subscript with get/set).
    -- With a root of value types (such as struct and enum), WritableKeyPath is inferred.
    -- With a root of reference types (such as class), ReferenceWritableKeyPath is inferred.

We declare every property with let, so we get KeyPath as a result.

let userRole = \User.role
// KeyPath<User, Role>

let streetAddress = \User.address?.street
// KeyPath<User, String?>
Enter fullscreen mode Exit fullscreen mode

first and debugDescription are read-only computed properties, so we also get KeyPath as a result.

let stringDebugDescription = \String.debugDescription
// KeyPath<String, String>

let firstInteger = \Array<Int>.first
// KeyPath<[Int], Int?>
Enter fullscreen mode Exit fullscreen mode

We get WritableKeyPath when reference array subscript because it is a read-write subscript (get/set).

subscript(index: Int) -> Element { get set }

let firstIndexInteger = \[Int][0]
// WritableKeyPath<[Int], Int>
Enter fullscreen mode Exit fullscreen mode

If we change the name property to var, we will get WritableKeyPath when reference \User.name.

struct User {
    var name: String
}

\User.name
// WritableKeyPath<User, String>
Enter fullscreen mode Exit fullscreen mode

If we change User to class, key path to var and let will be ReferenceWritableKeyPath and KeyPath, respectively.

class User {
    var name: String
    let email: String

    init(name: String, email: String) {
        self.name = name
        self.email = email
    }
}

\User.name
// ReferenceWritableKeyPath<User5, String>

\User.email
// KeyPath<User, String>
Enter fullscreen mode Exit fullscreen mode

Next Article: KeyPath Usage in Swift

Previous Article: KeyPath in Swift


Contacts
I have a clear focus on time-to-market and don't prioritize technical debt. And I took part in the Pre-Sale/RFX activity as a System Architect, assessment efforts for Mobile (iOS-Swift, Android-Kotlin), Frontend (React-TypeScript) and Backend (NodeJS-.NET-PHP-Kafka-SQL-NoSQL). And I also formed the work of Pre-Sale as a CTO from Opportunity to Proposal via knowledge transfer to Successful Delivery.

๐Ÿ›ฉ๏ธ #startups #management #cto #swift #typescript #database
๐Ÿ“ง Email: sergey.leschev@gmail.com
๐Ÿ‘‹ LinkedIn: https://linkedin.com/in/sergeyleschev/
๐Ÿ‘‹ LeetCode: https://leetcode.com/sergeyleschev/
๐Ÿ‘‹ Twitter: https://twitter.com/sergeyleschev
๐Ÿ‘‹ Github: https://github.com/sergeyleschev
๐ŸŒŽ Website: https://sergeyleschev.github.io
๐ŸŒŽ Reddit: https://reddit.com/user/sergeyleschev
๐ŸŒŽ Quora: https://quora.com/sergey-leschev
๐ŸŒŽ Medium: https://medium.com/@sergeyleschev
๐Ÿ–จ๏ธ PDF Design Patterns: Download

๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
sergeyleschev
Sergey Leschev

Posted on January 29, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Array of weak inย Swift.
swift Array of weak inย Swift.

February 4, 2023

Swift: Associated types.
swift Swift: Associated types.

February 4, 2023

KeyPath in Swift: Usage.
swift KeyPath in Swift: Usage.

January 29, 2023

Types of KeyPath in Swift.
swift Types of KeyPath in Swift.

January 29, 2023

KeyPath in Swift: Syntax.
swift KeyPath in Swift: Syntax.

January 29, 2023

ยฉ TheLazy.dev

About