How do I initialize a nested struct in SwiftUI? This struct will be populated after parsing JSON from a RESTAPI, but i want to make it available as Observable so my view can access it later when data is populated.
final class APIController: ObservableObject { 
@Published var iotshadow: IotShadow
IotShadow is the nested struct of a few levels. To line by line assign it a default value seems very excessive. Also if I leave it as optional IotShadow? then I don't seem to be allowed to access it as it complains that the value need to be unwrapped.
What would be the correct way to initialize a struct in this case? New to Swift but experienced Java/C programmer so maybe I am thinking in the wrong way here.
Thanks, Marcus


iotshadow = try! JSONDecoder().decode(IotShadow.self, from: jsonData) print("Reported Relay1: \(iotshadow.state.desired.RELAY1)")iotShadowin your print line. Definitely read up on how to use optional though. They are central to all development in Swift. It’s def important to know how and why to use them. 👍🏻