Swift - add object: array index out of range

When I try to add an item to my array, it gives me EXC BAD INSTRUCTION error and says

fatal error: Array index out of range

      

This is the code:

var tabelle : [[Actions]] = [[]]

func doSomething() {

    var results = self.fetch()

    var oldProjektName: String = results[0].projektName
    var count: Int = 0

    for item in results {
        if oldProjektName == item.projektName {
           tabelle[count].append(item)               
        } else {
            count++
            tabelle[count].append(item)
        }
        oldProjektName = item.projektName
    }
}

      

As long as count = 0 it doesn't give me an error, but when count = 1 the app crashes.

+3


source to share


2 answers


You have an array with one element: var tabelle : [[Actions]] = [[]]

This is why tabelle [0] works.



You need to add another array to the tabelle before you can use tabelle[1]

+5


source


Try



var tabelle = [[Actions]](())

0


source







All Articles