1

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.

11
  • 1
    You're iterating through object in objects but you have no objects variable declared... Commented Jul 26, 2016 at 13:36
  • I updated my question @pbush25 Commented Jul 26, 2016 at 13:38
  • @pbush25 This is not the source of the error. Commented Jul 26, 2016 at 13:38
  • there is no error if i'll try with one section so 1D array. Commented Jul 26, 2016 at 13:39
  • I suppose the start value of i is 0 so when you do self.district[self.i].append ... you are accessing self.district[0] but the array is still empty at this moment. Commented Jul 26, 2016 at 13:40

1 Answer 1

1

As mentioned in the comments, the array element with index self.i does not exist in the array self.district when you try to append at

self.district[self.i].append(object["district"] as! String)

and you have to append a new element to self.district first.

You should also get rid of all the forced casts as! which cause the program to crash if they fail. You code could then look like this:

var sections = [String]()
var districts = [[String]]()
if let objects = objects {
    for object in objects {
        if let town = object["town"] as? String,
            let dist = object["district"] as? String {

            if let idx = sections.indexOf(town) {
                districts[idx].append(dist)
            } else {
                sections.append(town)
                districts.append([dist])
            }
        }
    }
}

Alternatively (as suggested in a now deleted comment), store the information in a single array of a custom struct:

struct SectionInfo {
    let town: String
    var districts: [String]
}


var sections = [SectionInfo]()
if let objects = objects {
    for object in objects {
        if let town = object["town"] as? String,
            let dist = object["district"] as? String {

            if let idx = sections.indexOf({ $0.town == town }) {
                sections[idx].districts.append(dist)
            } else {
                sections.append(SectionInfo(town: town, districts: [dist]))
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.