Using Swift for my collectionView app. I'm reading a plist file to display the info in the arrays.
This is the code to read the plist which creates an array called sections[section #][item #] which lets me access items inside the arrays of the root.
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
I created var Items = [Any]()
to transfer everything from sections[][]
to the array Items so I can use it as a global array for my collectionsView but I get an error.
Type 'Any' has no subscript members
on the line print("specific item 2: - \(Items[0][1])")
How do I successfully transfer sections[][]
to Items
? I'm sure I'm not creating the array correctly. Thank you
Items is a ONE dimensional array.. you are trying to index it as a 2D array.. Change it to:
var Items = [[Any]]()
Now you can assign to it and append to it and index it as a 2D array. Each dimension requires a matching set of square brackets..
Example:
1D array: [Any]()
2D array: [[Any]]()
3D array: [[[Any]]]()