Keypaths allow you to refer to propertieswithout invoking them โ you hold a reference to the property itself, rather than reading its value.
Key paths ไธป่ฆไธ็จฎ่ฎ้ซ(้ๆๅ ถไป)
KeyPath: read-only access to a property.
WritableKeyPath: readwrite access to a mutable property with value semantics (so the instance in question also needs to be mutable for writes to be allowed).
ReferenceWritableKeyPath: can only be used with reference types, and provides readwrite access to any mutable property.
Keypaths in Swift have a few more types, which mostly revolve around type-erasure, like with Any. When you combine or allow multiple keypaths, for example in an array, you can use the PartialKeyPath and AnyKeyPath to construct types that fit multiple keypaths.
Swift Evolution โฉ
SE0161: Smart KeyPaths: Better Key-Value Coding for Swift
var user =User(username:"Hello")// โญ๏ธ type-erase to `AnyKeyPath`let keyPath: AnyKeyPath = \User.username // โญ๏ธ read-only/* -------- โญ๏ธ type-cast -------- */// โญ๏ธ if let ... as? ...iflet writableUsername = keyPath as? WritableKeyPath<User, String> { user[keyPath: writableUsername]="World"// โญ๏ธ read-write}// โญ๏ธ case let ... as ...switch keyPath {caselet a as KeyPath<User, String>:print(user[keyPath: a])caselet a as KeyPath<User, Int>:print(user[keyPath: a])default:print("Unknown keypath type")}