Learn Courses My Dashboard

Cannot use mutating member

The following code causes : Cannot use mutating member on immutable value
var newAddresses: [String]
HStack {
TextField(“Enter Address”, text: $inputAddress)
Button (action: { newAddresses.append(inputAddress)
inputAddress = “”}){Image(systemName: “plus”)}
}

Make newAddresses an @State property.


When posting code to these forums, place three backticks ``` on the line before your code and three backticks ``` on the line after your code so that it will be formatted properly. You can also highlight an entire code block and click the </> button on the toolbar to wrap the block for you.

This makes it far easier to read and also makes it easier for other posters to copy/paste the code in order to test solutions and such.

You might also notice that the quote marks are messed up in your original post because of not putting a code fence around your code. Discourse “helpfully” changes double quotes " to unicode double quotes “ and Xcode doesn’t like those, which interferes with people pasting your code into a test project. It’s not a big deal to correct them in Xcode but it’s a bit more friction.