不可变的值只有变异成员 [英] Immutable value only has mutating members

查看:126
本文介绍了不可变的值只有变异成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经声明使用 VAR 阵列和的init内填充它()。然而,当我尝试变异数组我得到的错误堆告诉我的数组是不可变的。缺少什么我在这里?

 结构甲板{
    VAR卡:卡[] = []    在里面 () {
        因为我在1 ... 4 {
            对于II在1 ... 13 {
                self.cards.append(卡(排名:Rank.fromRaw(二)!,套装:Suit.fromRaw(I)))
            }
        }
    }    FUNC洗牌(){
        VAR shuffledDeck:卡[] = []
        变种数= self.cards.count        因为我在1 ... 52 {
            VAR上限=计数 - 我
            VAR键= INT(arc4random_uniform(UInt32的(限制)));
            shuffledDeck.append(self.cards [关键])
            self.cards.removeAtIndex(键)
        }        self.cards = shuffledDeck
    }
}

我得到的错误:

 游乐场执行失败:错误:
<&REPL GT;:75:22:错误:类型的不可变的值'卡[]'不仅有变异的成员名为removeAtIndex
            self.cards.removeAtIndex(键)
                 ^ ~~~~~~~~~~~~~
<&REPL GT; 78:24:错误:无法在'自我'分配给'卡'
            self.cards = shuffledDeck


解决方案

A 结构被认为是值类型,因此它在默认情况下是不可改变的。如果你想用一个方法去改变它,你必须声明的方法突变。引用雨燕书:


  

结构和枚举是值类型。缺省情况下,
  值类型的属性不能从实例中修改
  方法。


  
  

然而,如果你需要修改结构的属性或
  一个特定的方法中枚举,你可以选择到变异
  行为该方法。然后该方法变异(即改变)
  从该方法中其属性,它使任何更改
  当该方法结束被写回到原来的结构。该
  方法也可以指定一个完全新的实例其隐含的自我
  财产,而这个新的实例将取代现有的当
  方法结束。


  
  

您可以通过前放置突变关键字选择参加这一行为
  在 FUNC 关键字该方法。


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

解决方案

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.

这篇关于不可变的值只有变异成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆