WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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

   

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)

   

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

   

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)

   

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?

   

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

   

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.

   

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()
    }

   

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

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.