|
Hi,
Searching for some help again. I think that I make things more complicated then needed. But I'm stuck on: "for a tough challenge – see the hints below! – make that detail screen contain how many times they have completed it, plus a button incrementing their completion count.". My idea was to make an list of all the times the user completed the task. And also a total of times. I struggled around with dates and got it working. My problem is now that I can't seem to add new dates to the array inside the struct. There is also on solution given, but I don't know how to get that working in my situation. Since the data also needs to be saved so that when the user closes the app and opens them again, the data is still there. Can somebody give me some hints on how I can do this? |
|
From what I understand, it should be possible? My struct:
From what I am saying, the mutating function should make it possible to add somthing to habTrack. somewhere alse I have in my code:
The error i'm (still) getting is : "Cannot use mutating member on immutable value: 'self' is immutable" |
|
@suzanne struck the wrong note on her struct.
We don't see the code where you defined the variable For example:
Because you explicitly tell the compiler that (1) is a constant (LET) the compiler will never, ever let you change it. Because you explicitly tell the compiler that (2) is a variable (VAR) the compiler will let you change what is stored by the variable. Keep codingPlease let us know how you solved this! |
|
I will add more of the code.. Tbh, I am a bit afraid of posting it since of the naming of functions, variables and others. And maybe it not all logical to others. The Struct is created as mentiond above. I'm creating an class
The three times var her where original let. I changed them to see if it makes a difference. My function to get the currentDate and formatted to how I want it:
One of the Views, This one works as an sheet
Where you see 'var habit = ' I had let before. Changing it to var doesn't seem to make an difference. My ContentView
And the last View
In this view where there is an Button and I try "habit.addTrack(add: curDate)" I get the error |
|
I think I fixed it. But not sure if it is realy correct. In my discriptionView I changed
to
Now it does work like I want to. Only not sure if it is really logic. |
|
Hi @suzanne! This is the way it should work. As you change it within view, i has to be marked as @State. Remember that when you change any property in the struct, the whole struct is created again. With @State wrapper the value is "stored" outside the struct so it will be the same upon other changes you implement and when view refreshes. E.g. if you view is refreshed and habit is not stored in @State it's gone for goods... So that is why compiler barks at you and won't let you go without it, because upon next view refresh it just crashes. |
|
@ygeras I don't completly follow what you are saying.. I notice that it doesn't work like I want and I do think that that is wat you are telling me. In my discriptionView I made the change mentioned in my last comment. I also did some Styling changes. Since I thought it was working so I wil give my discriptionView again:
If I click the Button, click safe, the data is saved. I can see it in the Foreach from this view. But When I go back to the previous view and then back to this one, the tracked data is gone :(. |
|
Hi @suzanne! I just made some small changes in your data model. You can adjust to your needs as well. As you can see the main change is in the observable object. Hope all is clear there. If not, post a question what is not clear.
|
|
Thankyou soo much!
I did try things with Index but did not get it working. I played around with your code and understand what it is doing and how. So thankyou! I think this will also help me with my next task: let user delete things from the habit list. I did make some small changes to it:
So the new entry will nou be added at the top. |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.