|
I'm trying to set the value of a @Binding var in the init of a view. I undstand how to do that for @State variables...but is it possible to handle this for @Binding? I get the error "Cannot assign value of type 'Int' to type 'Binding<Int>'" Background: I have a view that lists entities from a Core Data fetch request. I have two versions of that list view based on whether the user has chosen to view a simple list or a list based on a segmented fetch request. The "sort by" menu is at the main view and I would like that menu disabled when the fetch request is empty or has a low number of items to display. I need to pass the entity count of the fetch request up a level via a binding but am having trouble with the binding syntax. Thanks.
|
|
@Matt is trying to assign a value to a binding, but is pushing at the wrong end:
Take a moment and step away from the keyboard. Grab a few 3x5 cards to help you visualize. Think of a laboratory with two light switches. Each switch can be ON or OFF. The laboratory is ONE card with a status panel. Draw the light switches on the other two cards. If you change the value of one of the light switches, how to you send that value to the laboratory's main status panel? This is the job of the You create a subview to model the light switch. When the value in the light switch changes, you want the new value reflected in your laboratory. Paste into a new Xcode project:
|
|
A couple of points This is a
If you want to use the Preview the
If this is unclear Paul also explains it What is the @Binding property wrapper?. |
|
Thanks @Obelix and @NigelGee for your responses. I updated my example to correct the private @Binding. In @NigelGee's example, the binding between the parent view's @State returnView and the child view's @Binding returnView is made under the hood when the subview is called with "SubView(returnCount: $returnCount)" In many of Paul's examples he sets the value of an @State property wrapper in the view struct's init. In my example, I'm manipulating the @FetchRequest in the init based on an incoming NSPredicate. That part of the code works fine. I'm wondering if there is a method/ syntax for manually setting up that binding and setting the value from within the init. The value I want to set it to (packets.count) isn't available before the child view initializes. Something like:
|
|
Set the Binding amount from parent view as
Not sure that it then update You might want to try some like this
|
|
Thanks @NigelGee for your response. The syntax you used creates the binding - but you are right that updating the value of returnCount in the init throws all sorts of new errors. (Modifying state during view update, this will cause undefined behavior.) I'll play a little more, but I probably need to refactor this to manage the data differently. Thanks again. |
|
Got it to work with the help I received here. Here's the solution:
And in the child view:
Here's a stackoverflow I found this morning with the same question: https://stackoverflow.com/questions/68673348/return-swiftui-fetchedresults-count-from-child-view-to-parent-when-fetchreques Apple docs for .onReceive: https://developer.apple.com/documentation/swiftui/progressview/onreceive(_:perform:)/ Interestingly, .onReceive does not fire if the fetch request comes back empty. |
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.