[Swift 5.2] Struct + Property Wrapper didSet defect?

Absolutely, and this way of working is obviously correct from the POV of the property wrapper, but the didSet behaviour (in the context of the containing object) appears to be a regression. Unless I’m much mistaken, the property wrappers proposal didn’t suggest that their didSet semantics would vary from the default. Indeed, the point of property wrappers was to wrap additional behaviour without causing an adjustment in standard property behavior.

This causes much bigger problems if a referenced property in the didSet is not yet constructed as part of your initializer. Part of the reason didSet is not called in initialisers, but is called in subclass initialisers, is because of the inability to reason during initialization about the state of the containing object.

Yeah, I can reproduce it on 5.3 (Xcode 12 beta) but it behaves the same in 5.1 as well. Does it behave differently for you in an older version of the compiler?

Yeah, seems to be the case. That's unfortunate :disappointed:

My simple test shows @​State var willSet()/didSet() works now in Xcode 12 beta 4:

​import SwiftUI

struct AtStateDidSetWorkOrNot: View {
    @State private var aStateVar = 100 {
        willSet(newValue) {
            print("aStateVar willSet to \(newValue)")
        }
        didSet {
            print("aStateVar didSet from \(oldValue) to \(aStateVar)")
        }
    }

    var body: some View {
        VStack {
            Text("Trepidation #\(aStateVar)")
            Button {
                aStateVar = Int.random(in: 0...200)
                print("aStateVar = \(aStateVar)")
            }
            label: {
                Text("Change aStateVar")
            }
        }
    }
}

struct AtStateDidSetWorkOrNot_Previews: PreviewProvider {
    static var previews: some View {
        AtStateDidSetWorkOrNot()
    }
}
1 Like