使用Swift为我的collectionView应用程序。我正在读一个plist文件来显示数组中的信息。
这是读取plist的代码,它创建了一个名为sections [section#] [item#]的数组,它允许我访问根数组中的项目。
var Items = [Any]()
let url = Bundle.main.url(forResource:"items", withExtension: "plist")!
do {
let data = try Data(contentsOf:url)
let sections = try PropertyListSerialization.propertyList(from: data, format: nil) as! [[Any]]
for (index, section) in sections.enumerated() {
//print("section ", index)
for item in section {
//print(item)
}
}
print("specific item: - \(sections[1][0])") // (Section[] Item[])
print("section count: - \(sections.count)")
Items = sections
} catch {
print("This error must never occur", error)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
switch checkGroupChoice {
case 0:
print("From: Items")
print("specific item 2: - \(Items[0][1])")
return Items.count
我创建了var Items = [Any]()
来将所有内容从sections[][]
转移到数组项目,这样我就可以将它用作我的collectionsView的全局数组,但是我收到错误。
类型'Any'没有下标成员
在print("specific item 2: - \(Items[0][1])")
如何成功将sections[][]
转移到Items
?我确定我没有正确创建阵列。谢谢
答案 0 :(得分:1)
项目是一维数组..您尝试将其索引为2D数组..将其更改为:
var Items = [[Any]]()
现在您可以为其分配并附加到它并将其索引为2D数组。每个维度都需要一组匹配的方括号..
示例:
1D array: [Any]()
2D array: [[Any]]()
3D array: [[[Any]]]()