Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Immutable value only has mutating members

I've declared an array using var and filled it within an init(). Yet, when I try to mutate that array I get heaps of errors telling my the array is immutable. What am I missing here?

struct Deck {
    var cards: Card[] = []

    init () {
        for i in 1...4 {
            for ii in 1...13 {
                self.cards.append(Card(rank: Rank.fromRaw(ii)!, suit: Suit.fromRaw(i)!))
            }
        }
    }

    func shuffle () {
        var shuffledDeck: Card[] = []
        var count = self.cards.count

        for i in 1...52 {
            var limit = count - i
            var key = Int(arc4random_uniform(UInt32(limit)));
            shuffledDeck.append(self.cards[key])
            self.cards.removeAtIndex(key)
        }

        self.cards = shuffledDeck
    }
}

The errors I'm getting:

Playground execution failed: error:
<REPL>:75:22: error: immutable value of type 'Card[]' only has mutating members named 'removeAtIndex'
            self.cards.removeAtIndex(key)
                 ^     ~~~~~~~~~~~~~
<REPL>:78:24: error: cannot assign to 'cards' in 'self'
            self.cards = shuffledDeck
like image 745
Guido Bouman Avatar asked Jun 06 '14 06:06

Guido Bouman


1 Answers

A struct is considered a value type, so it’s immutable by default. If you want to change it using a method, you have to declare the method mutating. Quoting the Swift book:

Structures and enumerations are value types. By default, the properties of a value type cannot be modified from within its instance methods.

However, if you need to modify the properties of your structure or enumeration within a particular method, you can opt in to mutating behavior for that method. The method can then mutate (that is, change) its properties from within the method, and any changes that it makes are written back to the original structure when the method ends. The method can also assign a completely new instance to its implicit self property, and this new instance will replace the existing one when the method ends.

You can opt in to this behavior by placing the mutating keyword before the func keyword for that method.

like image 196
zoul Avatar answered Sep 19 '22 16:09

zoul