I have a tableview in which I want to have multiple sections that I load from my server so I need a 2D array.
What I've tried is this:
var sections = [String]()
var district = [[String]]()
if let objects = objects {
for object in objects {
let town = object["town"] as! String
if !self.sections.contains(town){
self.sections.append(town)
self.district[self.i].append(object["district"] as! String)
self.i += 1
}else{
let f = self.sections.indexOf(town)
self.district[f!].append(object["district"] as! String)
}
}
}
When I run it I got this message:
Index out of range
I tried to insert my elements with += but it doesn't even let me run it.
I also tried this:
self.district[0][0] = object["district"] as! String
And I got the same error.
object in objectsbut you have noobjectsvariable declared...self.district[self.i].append ...you are accessingself.district[0]but the array is still empty at this moment.