简体   繁体   中英

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.

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: "")!
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM