|
Short example, in reality there are many more properties in my use case:
Now I could append I was wondering if there's maybe a way that |
|
I don't think you can avoid writing a bunch of didSet blocks. Sometimes you gotta boil the plates when the plates need boiling. But, I did manage to have fun coming up with a really hacky solution to send the property through to the function when it's called. Just give them to the function as arguments. Originally I tried to get the variable name using Mirror but I gave up on that.
|
|
In case you can use
I thought it would be good if you had your own version of But I've never tackled to write a property wrapper which accesses "something" from the parent. I think this should be possible since Swift 5.4 and was not possible at the time of iOS 14. This is why Perhaps this article from John Sundell might be a path to implement a custom |
|
I would probably rethink the design of that |
|
If you need to react to a change in a property in a specific View, wouldn't it be better to use the .onChange() modifier or am I misunderstanding the point of your question? I guess where I am coming from is if the user does something that affects a property then that would be occuring in a View so that's why my suggestion is to use the .onChange() modifier since the Published property would be accessible from within the View via your ViewModel ObservableObject. |
|
Thanks for all the suggestions. The function I.e. "If one of these 11 properties changes (but not if one of the remaining 13 properties changes), call This maybe also better illustrates why However, the properties are not homogeneous in a way where it would be more appropriate to collect them in a separate I had already solved this with a custom property wrapper that replaces
is maybe the best way to achieve this at the moment. |
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!
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.