Array 的不可变值只有名为 append 的可变成员

Immutable value of Array only has mutating members named append

弹出此错误:

Immutable value of [PrizeItem] only has mutating members named append on the code line : prizesList.append(randomPrize)

 func openPrizeBox(){

        let prizeCycleCount = randomNumberInRange(8, 20) //<-Returns random Int
        let timeInterval = NSTimeInterval(prizeCycleCount)

        let prizesList : [PrizeItem] = []

        for var i = 0; i < prizeCycleCount; i++  {
            let randomPrize = prizeItems[randomNumberInRange(0, prizeItems.count-1)]
            prizesList.append(randomPrize) //Shows error here
        }

这是 PrizeItem 结构

struct PrizeItem {

enum Rank {
    case Ok
    case Good
    case Epic
    case Rare
    case ExtremelyRare
}

var name : String
var description : String

let rank : Rank


var identifier: String

}

数组:

let prizeItems : [PrizeItem] = [

PrizeItem(name: "Laser", description: "Test Item", rank: PrizeItem.Rank.Good, identifier: "laser")

]

如果你能帮助我,那就太好了! 非常感谢!!!

当您使用 let 时,您不能在这一行中更改或附加或添加新内容到此 variable.Therefore :

let prizesList : [PrizeItem] = []

应该改为

var prizesList : [PrizeItem] = []

重要仅当您需要此值保持不变时才使用 let