UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: How to fix 'self' is used before all stored properties are initialized?

Forums > Swift

    @ObservedObject var civDecode: CIVDecode
    @ObservedObject var icomVM: IcomVM

    init() {
        civDecode = CIVDecode(hostCivAddr: 0xe0)

        icomVM = IcomVM(host: "192.168.12.196",
                        controlPort: 50001,
                        serialPort: 50002,
                        audioPort: 50003,
                        user: "n8me",
                        password: "msrkmsrk",
                        computer: "MAC-MINI",
                        hostCivAddr: 0xe0,
                        civDecode: civDecode.decode)
    }

The above code is in a SwiftUI View.

I probably have an issue with my design, because Swift doesn't like it. I get the 'self' used... error on the last line of the init().

IcomVM is a view model that gets data over the network. CIVDecode is also a view model takes that data (via the civDecode() func), parses it and sets up some @Published properties used in the view. IcomVM also provides @Published properties, so both are to my mind ViewModels. The @Published properties provided by IcomVM have to do with the network connection. The @Published properties provided by CIVDecode relate to the remote device. Originally, I had CIVDecode as a property of IcomVM, but when the view referenced properties like icomVM.civDecode.property, the view displayed the correct value when it was updated (by one of the IcomVM properties updating), but the view didn't automatically update when CivDecode properties changed.

One solution would be to embed the members of the CivDecode class directly in the IcomVM class, but they really are two separate things. The only thing CivDecode needs to do it's job is data from IcomVM and could be usable with a different class providing the data (IOW, if I changed the connection to the remote device from UDP to USB, I could replace IcomVM which gets it's data over UDP datagrams with a different class that gets similar data over a USB connection).

I thought about making IcomVM publish the data (rather than the civDecode func) and having CivDecode subscribe to it, but I run into the same issue trying to pass the publisher between the two instances.

In similar cases (not in views), I have made one or more of these classes optional vars and then set them up later outside the init, but SwiftUI, complains about optional @Observable object. Also, if I don't initialize them in the view's init, when does the code get another change to assign them values?

I'm open to ideas (ideally a design pattern).

Mark

2      

Basically, the problem is that you're using self.civDecode when you initialize self.icomVM. You can easily fix this with a couple of local variables:

init() {
        localCivDecode = CIVDecode(hostCivAddr: 0xe0)

        localIcomVM = IcomVM(host: "192.168.12.196",
                        controlPort: 50001,
                        serialPort: 50002,
                        audioPort: 50003,
                        user: "n8me",
                        password: "msrkmsrk",
                        computer: "MAC-MINI",
                        hostCivAddr: 0xe0,
                        civDecode: localCivDecode.decode)
        self.civDecode = localCivDecode
        self.icomVM = localIcomVM
    }

3      

Thanks. That's a way of initializing properties that I'd never thought of, but it works great 👍

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.