我现在有以下代码:
var items: [[String, UIImage]] = [["String", UIImage(named: "image")]]
expected ']' in array type
,consecutive declarations on a line must be separated by ';'
,expected declaration
,class ViewController has no initializers
出错。
当我将其编码为var items: [[]] = [["String", UIImage(named: "image")]]
结果为Expected Element type
。
当我将[[]]
留在类型注释(var items = ...
)中时,会导致错误告诉我type of expression is ambiguous without more context
。
我该怎么做?我希望它是一个数组,而不是一个字典(上例中的字符串不是键,UIImage不是值)。
答案 0 :(得分:1)
问题是您尝试在数组中存储两个不同类型的值。您可以将它们放在元组中,然后将它们添加到数组中,如下所示:
var items: [(String, UIImage)] = [("String", UIImage(named: "image")!)]
答案 1 :(得分:-1)
对于二维数组,只需指定一个类型。
var items: [[AnyObject?]] = [["String", UIImage(named: "image")]]