Post

Replies

Boosts

Views

Activity

Strange object construction behaviour
I stumbled across this behaviour, and have excised it from my code, but I would like to understand why it was happening. The following code (with names changed to protect the innocent) was causing two instances of MyClass to be instantiated... why? It seems like the StateObject ought to be set to the newly instantiated wrappedValue, and then the global gObj optional set to refer to that object. Also, that appears not to be the case, and a second MyClass gets instantiated (demonstrated by a print or breakpoint in its init method). var gObj: MyClass? @main struct MyApp : App { @StateObject(wrappedValue: MyClass()) public var obj : MyClass 		init() { 				gObj = self.obj 		} var body: some Scene { WindowGroup { ContentView(obj: self.obj) } } }
1
0
329
Dec ’20