What's up with this latter syntax? Did I miss something? Is it a bug or by design?
struct S {
var value: [Int] = []
func foo(_: KeyPath<S, Int> = \.value.count) {}
func bar(_: KeyPath<S, Int> = \value.count) {}
}
This code compiles just fine. It's also interesting because the latter syntax works as a short-hand form for this property wrapper.
@propertyWrapper
struct Derived<Root, Value> {
var wrappedValue: Value {
get { fatalError() }
set { fatalError() }
}
β
let keyPath: ReferenceWritableKeyPath<Root, Value>
β
init(_ keyPath: ReferenceWritableKeyPath<Root, Value>) {
self.keyPath = keyPath
}
β
static subscript(
_enclosingInstance instance: Root,
wrapped wrappedKeyPath: ReferenceWritableKeyPath<Root, Value>,
storage storageKeyPath: ReferenceWritableKeyPath<Root, Self>
) -> Value {
get {
instance[keyPath: instance[keyPath: storageKeyPath].keyPath]
}
set {
instance[keyPath: instance[keyPath: storageKeyPath].keyPath] = newValue
}
}
}
β
β
class Parent: UIView {
private let label = UILabel()
β
@Derived(\label.text)
var text: String?
}
β
let instance = Parent()
instance.text // nil
instance.text = "foo"
cc @Joe_Groff