我正在制作一个待办事项列表应用程序,但是当我尝试从列表中删除某些内容时,xcode会给出一个错误,上面写着“致命错误:数组索引超出范围”.有人能告诉我,我的阵列出错了导致这种情况发生吗?
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() } } }
断点表示正在eventList.removeAtIndex(indexPath.row)中创建EXC_BAD_INSTRUCTION.