2

When some calculation happens in viewModel I want to present modal view. Normally I need to set some boolean binding for method:

.fullScreenCover(isPresented: $isGalleryPresented) {
    GalleryPickerView())
}

where isGalleryPresented, is @State definied in view. However browsing SO, i have found out that I could have property in viewModel:

@Published var isGalleryPresented = false

and then do something like this:

.fullScreenCover(isPresented: $viewModel.isGalleryPresented) {
    GalleryPickerView()
}

And this works just fine, although I don't know how. fullScreenCover method argument of type isPresented: Binding<Bool>, and I pass as far as I can tell a publisher. How does this work?

1 Answer 1

3

Your viewModel is a wrapped property of @ObservedObject, which provides binding via keypath subscript to wrapped observable object properties.

Here is a corresponding part of declaration:

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) @propertyWrapper @frozen public 
struct ObservedObject<ObjectType> : DynamicProperty where ObjectType : ObservableObject {

    /// A wrapper of the underlying observable object that can create bindings to
    /// its properties using dynamic member lookup.
    @dynamicMemberLookup @frozen public struct Wrapper {

        /// Returns a binding to the resulting value of a given key path.
        ///
        /// - Parameter keyPath  : A key path to a specific resulting value.
        ///
        /// - Returns: A new binding.
        public subscript<Subject>(dynamicMember keyPath: ReferenceWritableKeyPath<ObjectType, Subject>) -> Binding<Subject> { get }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.