UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Changing the value of an element in an Array

Forums > Swift

So I have code that will delete a student from the Student Array. However I also need to be able to change the Active Status (Bool) from true to false. (i.e. make the student inactive).

    func delete(_ student: Student) {
        if let index = students.firstIndex(of: student) {
            students.remove(at: index)
            save()
        }
    }

    func deactivate(_ student: Student) {
        //code for setting the active value to "false"
        student.active = false
    }

This code is inside the class Roster. It is called from the ViewRoster.swift file, and calls the deactivate method:

                        .swipeActions {
                            Button(role: .destructive) {
                                withAnimation {
                                    roster.delete(student)
                                }
                            } label: {
                                Label("Delete", systemImage: "trash")
                            }
                            Button {
                                withAnimation {
                                    roster.deactivate(_: student)
                                }
                            } label: {
                                Label("Repeat", systemImage: "repeat")
                            }
                            .tint(.blue)
                        }

When I try this I get the error: "Cannot assign to property: 'student' is a 'let' constant".

The active bool in the class is a var, not a let?

If you can offer any help it is greatly appreciated.

Thank you,

Bob

2      

Look at the deactivate function.

func deactivate(_ student: Student) {
        //code for setting the active value to "false"
        student.active = false
}

The student argument to the function is a let constant so you can't change it. If you want to change the student's status, make the student argument an inout argument.

func deactivate(_ student: inout Student)

2      

Thanks for the help, but I've tried this and it isn't working, I've changed the deactivate method, as you have recommended. Still getting an error.

    func deactivate(_ student: inout Student) {
        student.active = false
        save()
    }

The error is where the deactivate method is called:

                        .swipeActions {
                            Button(role: .destructive) {
                                withAnimation {
                                    roster.delete(student)
                                }
                            } label: {
                                Label("Delete", systemImage: "trash")
                            }
                            Button {
                                withAnimation {
                                    roster.deactivate(_: &student)
                                }
                            } label: {
                                Label("Repeat", systemImage: "repeat")
                            }
                            .tint(.blue)
                        }

Cannot pass immutable value as inout argument: 'student' is a 'let' constant

I must be missing something..

Bob

2      

Hello,

Your deactivate method just takes a student and your calling like so

roster.deactivate(_: &student)

remove the underscore and colon and try calling like this:

roster.deactivate(&student)

2      

How is that student you are using in ViewRoster.swift defined when you call roster.deactivate(&student)? Is it a property? is it in some loop?

2      

Readressing my first comment: If you defined student in a loop, rather than doing something like this:

for student in students {
    ...
}

you can do this:

for var student in students {
    ...
}

This is because for student in students is basically the same as for let student in students

2      

Sorry, I was out most of the day. The student is struct, that I initialize in the Roster class file. Not sure if this makes sense, but I was working with the CoolBeans App and trying to follow that same logic.

2      

Thanks to all of you who helped!

I figured it out (with your help)

    func deactivate(_ student: Student) {
        var studentCopy = student

        if let index = students.firstIndex(of: student) {
            students.remove(at: index)
        }

        studentCopy.active = false
        students.insert(studentCopy, at: 0)

        save()
    }

2      

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!

Archived topic

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.