我想在 UIImageView 类型的数组中附加值。需要添加的值取决于其他值。这是我的数组声明:
var images: [UIImage] = [UIImage(named: "")!]
这是我的代码:
let cardIndex = playableCards.index (where: { $0.currentTitle! == button.currentTitle! })
{
images[cardIndex] = [UIImage(named: "")!]
}
不,我想知道我在这里做错了什么。我只想将值添加到 cardIndex 告诉我的那个索引。我之前也尝试过添加这个:
while howManyCardsNeedsToBeUnlocked != 0{
images.append(UIImage(named: "")!)
howManyCardsNeedsToBeUnlocked - 1
}
所以图像总是有一个索引。感谢您的帮助。
最佳答案
playableCards.index (where: { $0.currentTitle! == button.currentTitle!})
返回一个可选值,因此您需要先解包该值才能使用它。我认为编译器给了你错误的错误,应该告诉你解开这个值。我在下面使用 if let
将其解包。
if let cardIndex = playableCards.index(where: { $0.currentTitle! == button.currentTitle! }) {
images[cardIndex] = UIImage(named: "")!
}
关于ios - 在其自身初始值内使用的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42844201/