LAST CHANCE: Save 50% on all my Swift books and bundles! >>

Bug with Swift 5.2 and didSet property observer on a @Published property wrapper?

Forums > SwiftUI

I banged my head for a day trying to figure out why my didSet property observer was not firing. After I could not figure it out I loaded up my code for iExpense for 100 Days of Swift UI. It wasn't firing there either. Then I loaded Paul's version of iExpense. It also didn't work.

I then booted off a Mojave boot drive and everything works as it should.

Then I did some searching and found this discussion:

Does anyone have any insight into this?



You might want to take a look at - SOLVED: Triggering didSet of a PropertyObserver confusion - on this forum a couple days ago.


Thanks. I am not sure how I missed that other post because I was looking for this topic before posting my own. However, that post apparently solves the problem by usine Combine. That does not change the fact that functionality that used to work prior to Swift 5.2 no longer works. I was working on the 100 days of SwiftUI day 77 challenge and coulnd't get my didSet code to work. That is when I went back to the iExpense app in project 7 to try to figure out what I was doing wrong and found it no longer worked either. Anyone following the project 7 lesson will not be able to get their code to work right now and they will have no idea why.


This appears to be a regression bug in Swift 5.2, but can be worked around.

The iExpense code includes the following action in reponse to the 'Save' button of the Add Expense view (your variable names might vary):

let item = ExpenseItem(name:, type: self.type, amount: actualAmount)

The trouble seems to be that the .append() operation does not trigger the property's didSet. But an assignment will, so can rewrite the second line above:

let item = ExpenseItem(name:, type: self.type, amount: actualAmount)
let newItemsList = self.expenses.items + [item]
self.expenses.items = newItemsList

Presumably this will be fixed in the next Swift/Xcode. Unless anyone can explain why this would be intended behaviour?


.... or after appending the new items you can just say

self.expenses.items = self.expenses.items

That creates a new version of itself and triggers didSet.


I'm confused. I just checked the project in both Xcode 11.4 and 11.4.1 and I can't reproduce the bug. IIUC adding an item shouldn't work in 11.4 which uses Swift 5.2, that's it? I guess it wasn't the bug, since it works for me in both.


I have fixed the bug and pulled the fix into 5.2. It will be available in the next Xcode 11.4 release.


@twostraws  Site AdminHWS+

@theblixguy Amazing! Thank you so much ❤️


Just an update - the fix is now available in Xcode 11.5 beta.


Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot now

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.