0

So I am using this code and I get the error: Immutable value of type 'Array' only has mutating members named 'append.' I have been unable to find any working solutions as of yet.

import Foundation
struct Roster
{
    var names: Array<String>  = []

    func add(name: String) {
        names.append(name)
    }
}

Thank you!

1 Answer 1

3

As the rule of immutable struct in swift, if you want to be able to modify the properties inside a method, you have to mark the method as mutating.

struct Roster
{
    var names: Array<String>  = []

    mutating func add(name: String) {
        names.append(name)
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.