你有两个问题(而且没有正则表达式!)
1.您没有创建array.你需要做:
var logoImages: [UIImage] = []
或
var logoImages: Array<UIImage> = []
或
var logoImages = [UIImage]()
或
var logoImages = Array<UIImage>()
2. If you want to add new objects to an array, you should use Array.append()
或 some of the equivalent syntactic sugar:
logoImages.append(UIImage(named: "logo.png")!)
或
logoImages += [UIImage(named: "logo.png")!]
或
logoImages += [UIImage(named: "logo.png")!, UIImage(named: "logo2.png")!]
您需要附加到数组中,因为(摘录from docs):
You can’t use subscript syntax to append a new item to the end of an
array. If you try to use subscript syntax to retrieve 或 set a value
f或 an index that is outside of an array’s existing bounds, you will
trigger a runtime err或. However, you can check that an index is valid
bef或e using it, by comparing it to the array’s count property. Except
when count is 0 (meaning the array is empty), the largest valid index
in an array will always be count - 1, because arrays are indexed from
zero.
当然,如果可能的话,你可以随时简化:
var logoImage: [UIImage] = [
UIImage(named: "logo1.png")!,
UIImage(named: "logo2.png")!
]
edit:请注意,UIImage现在有一个"failable" initializer,这意味着它返回一个可选值.我已经更新了所有代码位,以反映这一变化以及对数组语法的变化.