I'm having a lot of trouble developing a macOS App using SwiftUI.
(macOS version 11.4, Xcode version 12.5)
struct ContentView: View {
@ObservedObject var viewModel: ViewModel
var body: some View {
VStack {
Text("RX frequency: " + viewModel.status.receiverStatus.frequency)
Text("TX frequency: " + viewModel.status.transmitterStatus.frequency)
Text("Ambient °C : " + viewModel.status.climateSensorStatus["Ambient"]!.degrees)
The 3rd Text line dies with "Fatal error: Unexpectedly found nil while unwrapping an Optional value".
This is not happening in the ViewModel, so I'm thinking that @ObservedObject is incapable of dealing with dictionaries. Is this a bug, or am I asking too much?
Thank you tem. I've added a "fetch" into the ViewModel and this fixes the problem.
init() {
requestStatus()
}
As a noobie, it's all very confusing as to what order things get instantiated, so I get there by trial and error. Please could you explain how to write your suggested ["Ambient", default: "..."] subscript? I have so much to learn!
The nil-coalescing way works great. The other way doesn't, probably because "degrees" is not the only variable type in there. In fact, I'm making extensive use of dictionaries for other devices.
I've got my SwiftNIO server running, but I'm having a hard time with the client end. It's for a local network, so I don't want HTTPS - just TCP without the overhead. For now, I'm simulating the server request/responses within the client without the hardware and I can evolve the API more easily.