What magic in @State properties initialization? That Swift knows all props are done initializing and return and not execute any more code in init?

struct ContentViewCase4: View {
    @State var a: Int = 100
    @State var b: Int
    
    
    init() {
        // Swift seems to know that after this, all props are init'ed so it return after this and won't go any further
        b = 4444
        a = 1234    // this line is not executed
    }
    
    var body: some View {
        VStack {
            Text("a = \(a)")    // 100
            Text("b = \(b)")    // 4444
        }
    }
}

@hborla ?

Do we really need so many threads on this topic?

1 Like

Those other two thread started by me thinking this is a problem with optional. But turns out it’s the same for anything. So please ignore those those and continue here to figure what’s going.

This line is not ignored; it is a setter call instead of a call to State.init(wrappedValue:). The reason is because after b = 4444, all of self is initialized and all property wrapper assignments afterward are sets instead of initializations.

I believe assignments to @State variables inside of init have this sort of behavior where the updated value is not reflected later on, but I'm not familiar with the details.

1 Like

So this is not a bug, it’s just how @State work?