I have a Card class and a Player class.
In my Player class I have a function that takes a [Card] array and adds a Card to it.
However, when I call...
myCardArray.append(myCard)
...I get the error
Immutable value of type [Card] only has mutating values of name append
I can't figure out why this is? Why would this be immutable?
without more code, we can only guess what happened
sounds like you are doing something like
func addCard(_ myCardArray: [Card]) -> [Card] {
let myCard = Card()
myCardArray.append(myCard)
return myCardArray
}
the problem is that myCardArray
is immutable, as error message said, you can't modify it
you can declare myCardArray
mutable use var
func addCard(var _ myCardArray: [Card]) -> [Card] {
let myCard = Card()
myCardArray.append(myCard)
return myCardArray
}
or create a mutable copy of it
func addCard(_ myCardArray: [Card]) -> [Card] {
let myCard = Card()
var mutableMyCardArray = myCardArray
mutableMyCardArray.append(myCard)
return mutableMyCardArray
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With