WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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

   

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
    }

1      

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

   

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

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.