How does a SwiftUI View with a non-nil property decorated with @EnvironmentObject get around that property not being set during initialization? I know .environmentObject() does set the value but the Swift rules as I understand them would not allow that. I encountered this with the userData property of LandmarkList from the SwiftUI Tutorial:
https://developer.apple.com/tutorials/swiftui
On Twitter I was told: "Think of it as an implicitly unwrapped optional. It’s just initialized to nil until the value is available."
I agree this is the behavior I'm observing but I don't understand how the language rules are being manipulated to accomplish this. Normally a non nil property would have to be passed in through the initializer.
In my testings going through the Landmark tutorial, when I removed the call to .environmentObject() below I was surprised I did not get a compiler warning but instead a runtime error which feels very unSwift-like.
Thanks for any help or pointers.
https://developer.apple.com/tutorials/swiftui
On Twitter I was told: "Think of it as an implicitly unwrapped optional. It’s just initialized to nil until the value is available."
I agree this is the behavior I'm observing but I don't understand how the language rules are being manipulated to accomplish this. Normally a non nil property would have to be passed in through the initializer.
In my testings going through the Landmark tutorial, when I removed the call to .environmentObject() below I was surprised I did not get a compiler warning but instead a runtime error which feels very unSwift-like.
Code Block window.rootViewController = UIHostingController( rootView: LandmarkList() .environmentObject(UserData()) )
Thanks for any help or pointers.