Posts

Post not yet marked as solved
10 Replies
The above code (without NSObject) compiles and runs for me on Xode 11 GM and macos catalina beta 8.
Post not yet marked as solved
10 Replies
On another forum, I saw a comment that someone addressed this by making the class that conforms to ObservableObject a subclass of NSObject. I tried this change and it fixed the issue. I did not see anything in the documentation or the release notes indicating the class has to be a subclass of NSObject, but at least this apparently is a viable work around for now.>>>>import SwiftUIpublic class TestClass: NSObject, ObservableObject { @Published public var str: String = "Goodbye"}struct ContentView: View { @ObservedObject public var testObject: TestClass var body: some View { Text(testObject.str) .frame(maxWidth: .infinity, maxHeight: .infinity) }}
Post not yet marked as solved
10 Replies
I instantiate the object in the calling object (in this case AppDelegate.swift) and initialize it through the autogenerated init for ContentVIew. I did not include this code for brevity.import Cocoaimport SwiftUI@NSApplicationMainclass AppDelegate: NSObject, NSApplicationDelegate {.... func applicationDidFinishLaunching(_ aNotification: Notification) { ... window.contentView = NSHostingView(rootView: ContentView(testObject: TestClass())) ... }...}