|
Hello: I have a view that uses an environment variable containing an array of class objects. Each of these objects has a boolean property called 'Cached' that I need to toggle to allow the user to show either Image A or B on a button. Here is my code:
When the user clicks the button the image on the label does not change from "square.and.arrow.down" to "trash" (or viceversa) even though the property's value has in fact changed. I have checked this by attaching a "didSet" event on the property to output to the console once the property has changed. I am curious to know what my mistake is, why is the actual image not updating if the porperty has in fact changed values and it is set to advertise its changes as a published property of an observable object? I should note that I can get this to work if I change the Person class to be a Struct instead. But I have to say I don't know why that matters if with it being a class I am making direct changes to the correct object's property. Any help would be appreciated! |
|
I suspect that what's happening is, you're updating the |
|
@sbeitzel This was the problem. Thank-you for your reply. This is exactly why it would work when person was a struct because a struct's value semanticsso the change to one of its properties is also a change changes the whole object thus a change to the people array so @Published will send the notification and the View body will be recomputed. I kept it as a class and just created a toggle function inside of people that will call objectWillChange.send () when a specified person changes. thank-you! |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.
Link copied to your pasteboard.