|
Can anyone explain why this is not allowed... Given the following class:
And a class that has a
Why can't I bind to a
With the code
I get the error With the code:
I get the error Is this down to:
If the reason is 2 that seems like a pretty poor language feature to me. The more I use SwiftUI the more I find it requires a lot of work-arounds because things don't 'just work' Note: I have also trie making As a workaround I can modify
and change the referencing code to:
But that feels like way too much code to simply reference a value. |
|
Not at all difficult. At a basic level, the $ sign (when used for bindings) in SwiftUI in regards to Bindings is only used for @State and @Binding property wrappers which are declared at the top of the view you are in. Because you are using classes what you want to do is make your CandidateAttribute class and Wrapper class conform to ObservableObject -
Now because the TextField requires the Binding<String> argument you want to declare that particular variable in your class with the property wrapper @Published -
Your basically saying that I want this value to publish or notify any view using this value to that it has changed and furthermore allow that view to update based on the new value. Now because this variable is part of the CandidateAttribute class which in turn is a value inside the dictionary in the Wrapper class, is why we need to make sure that class conforms to ObservableObject as well. Now this is done you declare that dictionary property in the Wrapper class with the @Published property wrapper -
Finally in your view where you have declared properties for the view. I assume you have created a variable of type Wrapper? If you have you now have to decalare that as follows:
You are saying that this property is an Observed Object and i want to be notified of any @Published properties from that class. I am assuming that private function is part of the view you are using this on? If so then just get rid of the $ sign now at the front of candidateAttr.value and you should be good to go. Its worth doing a bit of reading about the different property wrappers for SwiftUI. They are so handy and simple to use once you know which one to use for what situation. Note: I have used a few assumptions about how you have your classes and code set up in doing this so i hope it works for your situation. Dave |
|
Thanks for your response but it does not work - it is one of the various 'workarounds' I had already tried before settling on the 'getValueBinding()' function which solves the issue even though it is ugly. The code is as follows:
The class
Elsewhere in the program I want to show a
The problem line is 'TextField("Set value", text: candidate.textValue)' as there appears to be no way to convert a |
|
Sorry about that. I thought it would of worked. Have you tried just using a binding variable just for the TextField? Then when you need to update the name property in your class you just save whatever is in the binding to that property?
Might be better doing it this way. Dave |
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 April 28th.
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.