TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

How to make a loop in a View with vars? Solved!!

Forums > SwiftUI

First of all, Let's say Hello to you all since this is my first post.

I'm going to leave the question as it is, and at the end i'm going to post the end result that solved the problem. The only thing that was missing was a binding "$" into the ForEach loop.

To the question....

I have a struct:

struct Activity: Hashable {
    var id: UUID
    var name: String
    var description: String
    var timesCompleted: Int

    mutating func addOne() {
        timesCompleted += 1
    }
}

I have a class:

class Activities:  Identifiable, ObservableObject {
    var activities = [Activity]()
    init(activities: [Activity] = [Activity]()) {
        self.activities = activities
    }
}

I have a function that appends one Activity at a time to the array inside Activities:

func save(title: String, description: String) {
        let id = UUID()
        var activity = Activity(id: id, name: title, description: description, timesCompleted: 1)

        activities.activities.append(activity)
    }

Up to that point everything is working and doing what it should. Cool, but when i try to use addOne() inside a ForEach loop insde my view, I get a "Cannot use mutating member on immutable value: 'activity' is a 'let' constant". So I now know that a ForEach loop creates lets instead of vars and that makes the struct inmutable. How can I make it so it gives vars back, or how can I use a for loop inside a View. My code inside this is:

  List {

                ForEach(activities.activities, id:\.self) { activity in

                    NavigationLink {
                        DetailedView(activities: activities, activity: activity)

                    } label: {
                        HStack{
                            Text(activity.name)
                            Spacer()
                            Text("\(activity.timesCompleted)")
                            Button {
                                activity.addOne() //this is the line that is not compiling
                            } label: {
                                Image(systemName: "plus")
                            }
                        }
                    }

                }

            }

So, now with the answer:

I have a struct; No changes here:

struct Activity: Hashable {
    var id: UUID
    var name: String
    var description: String
    var timesCompleted: Int

    mutating func addOne() {
        timesCompleted += 1
    }
}

I have a class: No changes here

class Activities:  Identifiable, ObservableObject {
    var activities = [Activity]()
    init(activities: [Activity] = [Activity]()) {
        self.activities = activities
    }
}

I have a function that appends one Activity at a time to the array inside Activities: No changes here either.

func save(title: String, description: String) {
        let id = UUID()
        var activity = Activity(id: id, name: title, description: description, timesCompleted: 1)

        activities.activities.append(activity)
    }

In the body of ContentView, I added three "$" to make the for loop work.

  List {

                ForEach($activities.activities, id:\.self) { $activity in

                    NavigationLink {
                        DetailedView(activities: activities, activity: $activity)

                    } label: {
                        HStack{
                            Text(activity.name)
                            Spacer()
                            Text("\(activity.timesCompleted)")
                            Button {
                                activity.addOne() //this is the line that is not compiling
                            } label: {
                                Image(systemName: "plus")
                            }
                        }
                    }

                }

            }

   

Hacking with Swift is sponsored by RevenueCat.

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.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.