user4258987
user4258987

Reputation:

Swift fatal error: array index out of range

I'm making a to-do list app but when I try to delete something from my list, xcode is giving me an error that says "fatal error: array index out of range". Can someone tell me what I'm doing wrong with my array that is causing this to happen?

import UIKit

class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return eventList.count

    }


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")

        cell.textLabel?.text = eventList[indexPath.row]

        return cell
    }

    override func viewWillAppear(animated: Bool) {

        if var storedEventList : AnyObject = NSUserDefaults.standardUserDefaults().objectForKey("EventList") {

            eventList = []

            for var i = 0; i < storedEventList.count; ++i {

                eventList.append(storedEventList[i] as NSString)
            }

        }
    }

    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

        if(editingStyle == UITableViewCellEditingStyle.Delete) {

            eventList.removeAtIndex(indexPath.row)

            NSUserDefaults.standardUserDefaults().setObject(eventList, forKey: "EventList")
            NSUserDefaults.standardUserDefaults().synchronize()


        }
    }
}

A breakpoint saying EXC_BAD_INSTRUCTION is being created at the eventList.removeAtIndex(indexPath.row)as well.

Upvotes: 4

Views: 8343

Answers (2)

x4h1d
x4h1d

Reputation: 6092

It means you are trying to access an index,indexPath.row, that exceed the eventList range. To fix this issue try:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    if(editingStyle == .Delete && indexPath.row < eventList.count) {
        eventList.removeAtIndex(indexPath.row)
        tableView.reloadData()

        NSUserDefaults.standardUserDefaults().setObject(eventList, forKey: "EventList")
        NSUserDefaults.standardUserDefaults().synchronize()
    }
}

Upvotes: 1

Martin R
Martin R

Reputation: 539685

It is not sufficient to remove the item from the data source array. You also have to tell the table view that the row is deleted:

if editingStyle == .Delete {

    eventList.removeAtIndex(indexPath.row)
    tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)

   // ...
}

Otherwise the table view will call the data source methods for the original number of rows, causing the out of range error.

Alternatively, you can call tableView.reloadData() when modifying the data source, but the above method gives a nicer animation.

Upvotes: 6

Related Questions