Slide 47
Slide 47 text
ObservedObject.Wrapper.ReadOnly
extension ObservedObject.Wrapper {
var readOnly: ReadOnly {
return ReadOnly(unsafeBitCast(self, to: ObjectType.self)) //
}
@dynamicMemberLookup
struct ReadOnly {
private let object: ObjectType
init(_ object: ObjectType) {
self.object = object
}
subscript(dynamicMember keyPath: KeyPath) -> Binding {
Binding(
get: { self.object[keyPath: keyPath] },
set: { _ in assertionFailure("Read-only") }
)
}
}
}