|
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. |
SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!
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.