🔰Key Path
Keypaths allow you to refer to properties without 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.
Examples
👉 replit
Last updated
Was this helpful?