diff --git a/Sources/PerceptionCore/SwiftUI/Bindable.swift b/Sources/PerceptionCore/SwiftUI/Bindable.swift index 87dd5f4..a108c68 100644 --- a/Sources/PerceptionCore/SwiftUI/Bindable.swift +++ b/Sources/PerceptionCore/SwiftUI/Bindable.swift @@ -32,7 +32,7 @@ @available(visionOS, unavailable) @dynamicMemberLookup @propertyWrapper - public struct Bindable { + public struct Bindable { @ObservedObject fileprivate var observer: Observer /// The wrapped object. @@ -52,27 +52,31 @@ dynamicMember keyPath: ReferenceWritableKeyPath ) -> Binding where Value: AnyObject { #if DEBUG && canImport(SwiftUI) - $observer.object[ - isPerceptionTracking: _PerceptionLocals.isInPerceptionTracking, - keyPath: keyPath - ] + func open(_: V.Type) -> Binding { + ($observer as! ObservedObject>.Wrapper).object[ + isPerceptionTracking: _PerceptionLocals.isInPerceptionTracking, + keyPath: unsafeDowncast(keyPath, to: ReferenceWritableKeyPath.self) + ] + } + guard let valueType = Value.self as? any Perceptible.Type else { fatalError() } + return open(valueType) #else $observer.object[dynamicMember: keyPath] #endif } /// Creates a bindable object from an observable object. - public init(wrappedValue: Value) where Value: AnyObject { + public init(wrappedValue: Value) where Value: AnyObject & Perceptible { self.observer = Observer(wrappedValue) } /// Creates a bindable object from an observable object. - public init(_ wrappedValue: Value) where Value: AnyObject { + public init(_ wrappedValue: Value) where Value: AnyObject & Perceptible { self.init(wrappedValue: wrappedValue) } /// Creates a bindable from the value of another bindable. - public init(projectedValue: Bindable) where Value: AnyObject { + public init(projectedValue: Bindable) where Value: AnyObject & Perceptible { self = projectedValue } }