简体   繁体   English

在其初始值内使用的变量

[英]Variable used within its own initial value

I want to append values inside of an array of the type UIImageView. 我想在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. 我只想向cardIndex告诉我的索引添加值。 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. playableCards.index (where: { $0.currentTitle! == button.currentTitle! })返回一个可选值,因此您需要先解开该值,然后才能使用它。 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我将其解开。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift变量在其自己的初始值内使用 - Swift Variable used within its own initial value Swift错误“变量在其初始值内使用” - Swift Error “Variable used within its own initial value” 错误:变量在其自身的初始值内使用? - Error: Variable used within its own initial value? Swift Error:变量在其自己的初始值内使用 - Swift Error: Variable used within its own initial value 在Swift 5中以其自身初始值使用的变量 - Variable used within its own initial value in Swift 5 删除NotificationCenter的观察器-“变量在其自身的初始值内使用” - Remove observer for NotificationCenter - “Variable used within its own initial value” 变量名与函数名冲突导致“变量在其自身的初始值内使用” - Variable name conflicts with function name leads to “Variable used within its own initial value” Swift:创建iOS框架并导入框架错误:变量在其初始值内使用 - Swift: Creating iOS framework and importing the framework error : Variable used within its own initial value 在Swift中使用块给出错误“变量在其自己的初始值内使用” - Use block in Swift giving error “Variable used within its own initial value” Swift 变量在使用#selector时在其自己的初始值内使用 - Swift Variable used within its own initial value while using #selector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM