Post

Replies

Boosts

Views

Activity

Preview Macro fails to load when using ObservedObject
After spending some time trying to understand the difference between StateObject and ObservedObject, I learned that ObservedObject has to be added to child views. I want to be able to use my child view in a variety of parent views, so I decided to make the child view as a separate file and use ObservedObject. However, the preview fails to load and returns "Failed to build the scheme" error. I recreated the project into something as simple as possible. Let me provide some code here (comments are not copied here): ViewModel: This will house all of the data that will be updated by the View. I have no child properties for the sake of simplicity. import Foundation public class ChildViewModel: ObservableObject { } Now here is a child view that was added as a separate file: import SwiftUI struct ChildView: View { @ObservedObject var viewModel: ChildViewModel var body: some View { Text("Hello, World!") } } #Preview { @StateObject var cViewModel = ChildViewModel() ChildView(viewModel: cViewModel) } This is as simple as it gets and it still gives me problems. Coming from C#/XAML world, this has been a massive pain to figure out. I scoured every forum I could, used Apple's own documentation and have not found a single post that can explain what I am doing wrong.
1
0
677
Dec ’23