[英]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.