|
Hi everyone! I’m struggling with a point of the challenge of Day 47 of SwiftUI. I implemented the view to show the title, the description and the count of the tracked activity, but I have a problem when I implement the button to increase the repetition count. If i go like that:
I receive the following error:
I implemented a struct for the tracked item and created a class with an array of such structs, and I guess that the error stem from the fact that a struct is an immutable copy of the values, so how can I implement this increase button as that it can update the value of the struct in the array? I passed the struct to the View via a NavigationLink nested in a ForEach that iterates over every element of the items array, should I pass the class? |
|
Hi Gabriele, I can't fully understand the part below your error message without seeing that actual code. But your code sample and your error message point that itemDetail is a property of the View you're working in and it has not been marked as @State var (which would let "Left side of mutating operator" to be mutable. |
|
Hi Daniel, thank you for your answer! Here is the code of the Detail view:
And here is the ContentView which links to the Detail View:
And these are my struct and Class:
Right now, I pass the struct to the detail view, but even if I update it (thank you for the @State tip! It worked!) then it's not update in the actual item in the class. How can I pass the struct back and forth between the views? Thank you very much! |
|
Hi Gabriele, For your DetailView to be able to send back changes, that property would have to be a Binding (to a property in ContentView). Unfortunately, it cannot be bound to a data element of a ForEach loop that's iterating over objects. The least amount of changes to your code to get what you want: Instead of passing in an item to DetailView, the loop would pass the index of the current item + provide the DetailView with trackedObjects through the environment. Then any changes that DetailView does to the environment object will be reflected in the original. P.S. using "count" as a name for your own properties is not prohibited, but might get you into unexpected situations that take time to figure out. For example:
If you named yours "counter" or anything else, you'd be sure not to get tangled with "count"
|
|
Hi, Sorry for the late answer. Thank you very much for your detailed answer, it's very helpful. Maybe this topic si a bit more advanced that what I've been working on, but that's a great occasion to learn. I'll try this as soon as I get to the computer, and report back. Thank you very much! |
|
Hi Daniel, It worked! Thank you so much! Out of curiosity, how would you have implemented on your own if you had to start from scratch? Would you have done something similar? |
BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.
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.