UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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: https://forums.swift.org/t/swift-5-2-struct-property-wrapper-didset-defect/34403

Does anyone have any insight into this?

-Sean

3      

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

3      

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.

3      

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: self.name, type: self.type, amount: actualAmount)
self.expenses.items.append(item)

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: self.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?

3      

.... 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.

3      

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.

3      

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

3      

@twostraws  Site AdminHWS+

@theblixguy Amazing! Thank you so much ❤️

3      

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

3      

Hacking with Swift is sponsored by Superwall

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.