0

This error pops up :

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
        }

This is the PrizeItem Struct

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

}

Array:

let prizeItems : [PrizeItem] = [

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

]

If you can help me that would be great! Thank you very much!!!

2
  • Did you read about let vs var in the Swift book? Commented Aug 26, 2015 at 16:40
  • @MartinR What do i need to do? Commented Aug 26, 2015 at 16:43

1 Answer 1

1

When you use let you cannot change or append or add new things to this variable.Therefore in this line :

let prizesList : [PrizeItem] = []

should be change to

var prizesList : [PrizeItem] = []

Important Using let only when you need to this value to be constant

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! You made my day. That was a stupid mistake. XD I spent a long hour removing and editing code. Thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.