Swift致命错误:数组索引超出范围

您所在的位置:网站首页 错误的数组或者数组超出界限 Swift致命错误:数组索引超出范围

Swift致命错误:数组索引超出范围

2024-05-21 05:15| 来源: 网络整理| 查看: 265

百度翻译此文   有道翻译此文 问题描述

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.

推荐答案

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.

其他推荐答案

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() } }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3