我想要做的是将firstCard
的图像设置为与名称对应于firstCardString
的图像文件相同。无法转换类型'[String]?'的值到期望的参数类型“字符串”
例如,在下面的情况下,代码可以设置self.firstCard.image
以显示名为“card1”的图像,如果它随机选取它(为了简洁,我已经剪切了数组的其余部分,完整的东西包含52个对象)。
var deckArray = [
"card1": ["Bear","Ball"],
"card2": ["Bear","Ball"],
"card3": ["Bear","Ball"],
"card4": ["Bear","Ball"],
"card5": ["Bear","Ball"],
"card6": ["Bear","Ball"],
"card7": ["Bear","Ball"],
]
let firstRandomNumber = Int(arc4random_uniform(52))+1
let firstCardString = deckArray["card\(firstRandomNumber)"]
self.firstCard.image = UIImage(named: firstCardString)
相反,我发现了以下错误:
Cannot convert value of type '[String]?' to expected argument type 'String'
我不完全知道这是什么错误消息意味着,什么是[String]?
?
但是,如果使用'self.firstCard.image =的UIImage(命名为:“卡\(firstRandomNumber)”)',不就是企图firstCard设置是只之间的随机数的图像名称1和52,而不是将其设置为与该索引号相对应的键(“card4”)? – TheDudeGuy
图像名称将为“card1”或“card3”或“card5”。请在名称中看到“卡”字样?紧接着是'\(firstRandomNumber)',它将在“card”一词后面插入数字。 – andyvn22
@ andyvn22请回答相同的相关问题在这里http://stackoverflow.com/questions/42733225/cannot-convert-the-value-of-type-string-to-expected-argument-type-string –