3

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

fatal error: Array index out of range

That 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 does not give me an error but when count = 1 then the app crashes.

2 Answers 2

6

You have an array with one element: var tabelle : [[Actions]] = [[]] That is why tabelle[0] is working.

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!! Changing table[count].append(item) in the else statement to var array : [Actions] = [item]; tabelle.append(array) solved the problem
-1

Try

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

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.