# \[Swift 5.2\] Struct + Property Wrapper didSet defect?

**URL:** <https://forums.swift.org/t/swift-5-2-struct-property-wrapper-didset-defect/34403>\
**Category:** Using Swift\
**Created:** [March 9, 2020, 8:16am UTC](https://forums.swift.org/t/swift-5-2-struct-property-wrapper-didset-defect/34403 "2020-03-09T08:16:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rod\_Brown](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rod_brown/32/425_2.png) [@Rod\_Brown](https://forums.swift.org/u/Rod_Brown)\
**Post date:** [March 9, 2020, 8:16am UTC](https://forums.swift.org/t/swift-5-2-struct-property-wrapper-didset-defect/34403/1 "2020-03-09T08:16:54Z")

</div>

Hi all,

Something I noticed in Swift 5.2 in the latest betas, and the latest snapshots, when you use a property wrapper for a struct, and you set a value inside the struct, a didSet does not get called. For example:

```swift
import Combine

struct MyStruct {
    var details: String
}

class MyClass {

    @Published var myStruct = MyStruct(details: "Test") {
        didSet {
            print("My struct was set")
        }
    }

}

let myClass = MyClass()

myClass.myStruct.details = "test" // This should print "My struct was set", but doesn't.
myClass.myStruct = MyStruct(details: "test") // This continues to print "My struct was set".

```

Is this change in behaviour expected? Seems extremely weird to me, and is causing a few breaking defects inside our codebase.

Thanks,

Rod

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [March 9, 2020, 8:50am UTC](https://forums.swift.org/t/swift-5-2-struct-property-wrapper-didset-defect/34403/2 "2020-03-09T08:50:43Z")

</div>

This might be [SR-12178](https://bugs.swift.org/browse/SR-12178), I have a fix for it [here](https://github.com/apple/swift/pull/29931). If not, then it might be a different bug.

---

<div class="post-metadata">

**Author:** ![dlbuckley](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dlbuckley/32/14986_2.png) [@dlbuckley](https://forums.swift.org/u/dlbuckley)\
**Post date:** [March 9, 2020, 8:53am UTC](https://forums.swift.org/t/swift-5-2-struct-property-wrapper-didset-defect/34403/3 "2020-03-09T08:53:56Z")

</div>

You aren't the first one to notice this. There is a bug report for the exact issue here: [[SR-12089] Swift 5.2 snapshot: didSet not called · Issue #54525 · apple/swift · GitHub](https://bugs.swift.org/browse/SR-12089) and another report about it on the forums here: [Property wrapper observer not firing](https://forums.swift.org/t/property-wrapper-observer-not-firing/33707)

Unfortunately it doesn't look like its been picked up yet and the closer to the 5.2 release we get the less chance it will get fixed. I'm tagging in @beccadax as he was the one who created the internal Apple tracking ticket.

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [March 9, 2020, 9:00am UTC](https://forums.swift.org/t/swift-5-2-struct-property-wrapper-didset-defect/34403/4 "2020-03-09T09:00:11Z")

</div>

That looks like the one I mentioned before - I have linked it to my fix! And yeah, it might not make 11.4 but might be available in a point release (like 11.4.1) depending on when my PR is merged.

---

<div class="post-metadata">

**Author:** ![Rod\_Brown](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rod_brown/32/425_2.png) [@Rod\_Brown](https://forums.swift.org/u/Rod_Brown)\
**Post date:** [March 9, 2020, 9:12am UTC](https://forums.swift.org/t/swift-5-2-struct-property-wrapper-didset-defect/34403/5 "2020-03-09T09:12:13Z")

</div>

This is a pretty glaring bug that will blindside anyone who’s relying on these semantics. The only way I noticed is via unit tests failing. I sure hope this makes it into 11.4.

---

<div class="post-metadata">

**Author:** ![Panajev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/panajev/32/1227_2.png) [@Panajev](https://forums.swift.org/u/Panajev)\
**Post date:** [March 9, 2020, 9:27am UTC](https://forums.swift.org/t/swift-5-2-struct-property-wrapper-didset-defect/34403/6 "2020-03-09T09:27:40Z")

</div>

How can we get an official Apple answer about this being addressed for Xcode 11.4? It seems like a regression in observability semantics warrants delaying Xcode 11.4 until it is fixed.

---

<div class="post-metadata">

**Author:** ![beccadax](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/beccadax/32/34217_2.png) [@beccadax](https://forums.swift.org/u/beccadax)\
**Post date:** [March 10, 2020, 7:48am UTC](https://forums.swift.org/t/swift-5-2-struct-property-wrapper-didset-defect/34403/7 "2020-03-10T07:48:01Z")

</div>

> [@dlbuckley](#):
>
> I'm tagging in @brentdax as he was the one who created the internal Apple tracking ticket.

Don't read too much into that—I create nearly all of the radars. 🙂

> [@suyashsrijan](#):
>
> This might be [SR-12178](https://bugs.swift.org/browse/SR-12178), I have a fix for it [here](https://github.com/apple/swift/pull/29931).

Yes, looking at SIL dumps, I think the modify accessor is the problem in all of these bugs (and I thought so before I looked at your PR).

I don't know when this bug will be fixed (i.e. this is not an "official Apple answer"), but for the `@Published` examples at least, there's a straightforward workaround: subscribe to the property's projected value (the matching `$` property), which is a `Publisher` that publishes changes to the property, and tie your `didSet` logic to that.

---

<div class="post-metadata">

**Author:** ![BobbyRohweder](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bobbyrohweder/32/11033_2.png) [@BobbyRohweder](https://forums.swift.org/u/BobbyRohweder)\
**Post date:** [March 27, 2020, 7:47pm UTC](https://forums.swift.org/t/swift-5-2-struct-property-wrapper-didset-defect/34403/8 "2020-03-27T19:47:55Z")

</div>

Also experiencing this issue when using `+=` with a property wrapped int.

Using the released version of Xcode 11.4  
This bug completely broke a piece of our app.

Switching it to `value = value + 1` works but doesn't seem ideal.

Example code:

```swift
import Combine

class MyClass {

    @Published var value = 4 {
        didSet {
            print("Value was set to \(value)")
        }
    }

}

let myClass = MyClass()

myClass.value = 5 // This prints "Value was set to 5"
myClass.value += 1 // This should print "Value was set to 6", but doesn't.
myClass.value = myClass.value + 1 // This prints "Value was set to 7"

```

---

<div class="post-metadata">

**Author:** ![MasasaM](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/masasam/32/11970_2.png) [@MasasaM](https://forums.swift.org/u/MasasaM)\
**Post date:** [March 28, 2020, 2:15pm UTC](https://forums.swift.org/t/swift-5-2-struct-property-wrapper-didset-defect/34403/9 "2020-03-28T14:15:06Z")

</div>

Here's a minimum case.  
This bug will occur with mutating operations.

```swift
@propertyWrapper
struct A<V> {
    var wrappedValue: V
}

class C {
    @A var x = 0 {
        didSet { print(x) }
    }
}

extension Int {
    mutating func f() {
        self = 1
    }
}

C().x.f() // doesn't print
C().x += 2 // doesn't print
C().x = 3 // prints '3'

```

---

<div class="post-metadata">

**Author:** ![DnV1eX](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dnv1ex/32/6601_2.png) [@DnV1eX](https://forums.swift.org/u/DnV1eX)\
**Post date:** [April 2, 2020, 7:34pm UTC](https://forums.swift.org/t/swift-5-2-struct-property-wrapper-didset-defect/34403/10 "2020-04-02T19:34:12Z")

</div>

Same here! I cannot believe they knew about the bug months ago and released Swift 5.2 without the fix. It should be considered not a regular medium-priority bug, but critical functionality regression!

---

<div class="post-metadata">

**Author:** ![minuscorp](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/minuscorp/32/8404_2.png) [@minuscorp](https://forums.swift.org/u/minuscorp)\
**Post date:** [April 11, 2020, 7:47pm UTC](https://forums.swift.org/t/swift-5-2-struct-property-wrapper-didset-defect/34403/11 "2020-04-11T19:47:34Z")

</div>

This is clearly an example of `_modify` being used now instead of get and set. That it's the reason why `+=` does not work but `value = value + 1` does.

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [April 13, 2020, 5:54pm UTC](https://forums.swift.org/t/swift-5-2-struct-property-wrapper-didset-defect/34403/12 "2020-04-13T17:54:33Z")

</div>

This is now [fixed](https://github.com/apple/swift/pull/29931) on master and I have [opened](https://github.com/apple/swift/pull/30994) a PR to pull the fix into 5.2 as well.

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [April 29, 2020, 7:38pm UTC](https://forums.swift.org/t/swift-5-2-struct-property-wrapper-didset-defect/34403/13 "2020-04-29T19:38:24Z")

</div>

I can confirm the fix has made its way to Xcode 11.5 beta.

---

<div class="post-metadata">

**Author:** ![gui\_dos](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gui_dos/32/1020_2.png) [@gui\_dos](https://forums.swift.org/u/gui_dos)\
**Post date:** [April 30, 2020, 12:58am UTC](https://forums.swift.org/t/swift-5-2-struct-property-wrapper-didset-defect/34403/14 "2020-04-30T00:58:13Z")

</div>

I had to use a logical negation instead of toggle() if I wanted to store the new boolean value of a SwiftUI @Published property in UserDefaults.

With the new Swift 5.2.4 in Xcode 11.5 beta I confirm you can use toggle() again.

```
class Settings: ObservableObject {
    @Published var mutedAudio: Bool = UserDefaults.standard.bool(forKey: "mutedAudio") {  
        didSet { UserDefaults.standard.set(self.mutedAudio, forKey: "mutedAudio") }  
    } 
}

Button(action: {  
    self.settings.mutedAudio.toggle() 
    // self.settings.mutedAudio = !self.settings.mutedAudio // workaround in iOS 13.4
}) {  
    Image(systemName: settings.mutedAudio ? "speaker.slash.fill" : "speaker.2.fill").resizable().frame(width: 24, height: 24)  
}

```

---

<div class="post-metadata">

**Author:** ![royhsu](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/royhsu/32/4589_2.png) [@royhsu](https://forums.swift.org/u/royhsu)\
**Post date:** [April 30, 2020, 7:09am UTC](https://forums.swift.org/t/swift-5-2-struct-property-wrapper-didset-defect/34403/15 "2020-04-30T07:09:09Z")

</div>

I'm not sure if @State was treated differently from @Published property wrapper.  
I tested the following code both with @State and @Published in Xcode 11.5 beta (11N605c), but only observers (willSet, didSet) for @Published were triggered.  
And I'm also not sure whether this is related to SwiftUI implementation detail or the same bug related to this post?  
Maybe I missed something obvious.

```swift
struct ContentView: View {
  @State
  private var isOn = false {
    willSet {
      print("@State isOn willSet:", isOn) // not working
    }
    didSet {
      print("@State isOn didSet:", isOn) // not working
    }
  }
  @ObservedObject
  private var store = ContentStore()
  var body: some View {
    Form {
      Toggle(isOn: $isOn) {
        Text("The toggle for @State isOn")
      }
      Toggle(isOn: $store.isOn) {
        Text("The toggle for @Published isOn")
      }
    }
  }
}

final class ContentStore: ObservableObject {
  @Published
  var isOn = false {
    willSet {
      print("@Published isOn willSet:", isOn) // working
    }
    didSet {
      print("@Published isOn didSet:", isOn) // working
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Rod\_Brown](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rod_brown/32/425_2.png) [@Rod\_Brown](https://forums.swift.org/u/Rod_Brown)\
**Post date:** [July 11, 2020, 4:47am UTC](https://forums.swift.org/t/swift-5-2-struct-property-wrapper-didset-defect/34403/16 "2020-07-11T04:47:11Z")

</div>

Hi @surashsrijan,

I'm just curious did that fix change the semantics of the `didSet` operator to fire even before initialisation has finished?

Something I noticed:

```swift

class MyClass: MySuperClass {

    private var itemHasChanged = false

    @Published private var myPublishedItem: Item? {
        didSet {
            itemHasChanged = true // This shouldn't fire in the initialiser, but does?
        }
    }

    init(item: Item?) {
        self.item = item
        super.init()
    }
}

```

If the value is marked as `@Published`, this fires in the initialiser. If it isn't marked with a property wrapper, it doesn't. Is this expected?

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [July 11, 2020, 3:54pm UTC](https://forums.swift.org/t/swift-5-2-struct-property-wrapper-didset-defect/34403/17 "2020-07-11T15:54:48Z")

</div>

Could you post a full reproducer?

---

<div class="post-metadata">

**Author:** ![Rod\_Brown](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rod_brown/32/425_2.png) [@Rod\_Brown](https://forums.swift.org/u/Rod_Brown)\
**Post date:** [July 12, 2020, 11:40am UTC](https://forums.swift.org/t/swift-5-2-struct-property-wrapper-didset-defect/34403/18 "2020-07-12T11:40:13Z")

</div>

Copy and paste this into a playground:

```swift
import Combine

class MyClass {

    @Published var publishedInt = 5 {
        didSet { print("Published int updated") }
    }

    var unpublishedInt = 5 {
        didSet { print("Unpublished int updated") }
    }

    init(publishedInt: Int, unpublishedInt: Int) {
        self.publishedInt = publishedInt
        self.unpublishedInt = unpublishedInt
    }

}

let example = MyClass(publishedInt: 4, unpublishedInt: 4)

```

When run, the published print is performed, but the unpublished print is not.

---

<div class="post-metadata">

**Author:** ![jonprescott](https://avatars.discourse-cdn.com/v4/letter/j/e36b37/32.png) [@jonprescott](https://forums.swift.org/u/jonprescott)\
**Post date:** [July 12, 2020, 3:55pm UTC](https://forums.swift.org/t/swift-5-2-struct-property-wrapper-didset-defect/34403/19 "2020-07-12T15:55:51Z")

</div>

It seems that for `@Published` variables, the default initialization counts as a setting (where you initialize it to '5'), thus when you change the value in the `init` method, the value changes and you invoke the `didSet`. The second `unpublishedInt` follows the standard Swift convention where `init` initializes the variable, no matter if it was previously initialized in a initial value statement. I guess the question is, "does a change in a variable's value in an `init` method count as a change for `@Published` or not?" Note that if you remove the initialization of `publishedInt`, no printout results.

The following shows consistent behavior between the two, but allows for default initialization to `5`:

```swift
import Combine

class MyClass {

	@Published var publishedInt : Int {
		didSet { print("Published int updated") }
	}

	var unpublishedInt : Int {
		didSet { print("Unpublished int updated") }
	}

	init(publishedInt: Int=5, unpublishedInt: Int=5) {
		self.publishedInt = publishedInt
		self.unpublishedInt = unpublishedInt
	}

}

let example = MyClass(publishedInt: 4, unpublishedInt: 4)

```

---

<div class="post-metadata">

**Author:** ![pyrtsa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pyrtsa/32/740_2.png) [@pyrtsa](https://forums.swift.org/u/pyrtsa)\
**Post date:** [July 12, 2020, 4:16pm UTC](https://forums.swift.org/t/swift-5-2-struct-property-wrapper-didset-defect/34403/20 "2020-07-12T16:16:56Z")

</div>

This pitch seems closely related and AFAICT explains your observed current behaviour: [Allow Property Wrappers with Multiple Arguments to Defer Initialization when wrappedValue is not Specified](https://forums.swift.org/t/proposal-allow-property-wrappers-with-multiple-arguments-to-defer-initialization-when-wrappedvalue-is-not-specified/38319)

Namely, wrapped properties get immediately initialised at their declaration if a default value is provided there; assigning them in the initialiser then counts as reassignment and that's why `didSet` gets (IMO somewhat unexpectedly) called.

If the pitched proposal were to be adopted then neither `didSet` would get called in your example.

[Next page](https://forums.swift.org/t/swift-5-2-struct-property-wrapper-didset-defect/34403.md?page=2)
