NoKey
NoKey

Reputation: 403

Variable used within its own initial value

I want to append values inside of an array of the type UIImageView. It values that needs to be added depends on other values. This is my declaration of my array:

var images: [UIImage] = [UIImage(named: "")!]

And this is my code:

            let cardIndex = playableCards.index (where: { $0.currentTitle! == button.currentTitle! })
            {
                images[cardIndex] = [UIImage(named: "")!]

            }

No I am wondering what I am doing wrong here. I just want to add values to that index of that cardIndex is telling me. I also tried adding this before:

    while howManyCardsNeedsToBeUnlocked != 0{
        images.append(UIImage(named: "")!)
        howManyCardsNeedsToBeUnlocked - 1
    }

So that images always have an index. Thank you for your help.

Upvotes: 0

Views: 795

Answers (1)

keithbhunter
keithbhunter

Reputation: 12324

playableCards.index (where: { $0.currentTitle! == button.currentTitle! }) returns an optional, so you need to unwrap the value before you can use it. I think the compiler is giving you the wrong error and should be telling you to unwrap the value. I unwrap it below with if let.

if let cardIndex = playableCards.index(where: { $0.currentTitle! == button.currentTitle! }) {
    images[cardIndex] = UIImage(named: "")!
}

Upvotes: 1

Related Questions