我在Swift中创建了一个ToDoList应用程序,它在三天前完美运行但是当我回到计算机时,我收到了此错误消息。致命错误:数组索引超出范围。有人能告诉我如何解决这个问题。
import UIKit
var date = [String]()
var todoItem = [String]()
class SwiftController: UIViewController{
@IBOutlet weak var table: UITableView!
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return date.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell{
let cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "cell")
cell.textLabel!.text = date[indexPath.row]
cell.detailTextLabel!.text = todoItem[indexPath.row]
return cell
}
func tableView(tableView:UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
if editingStyle == UITableViewCellEditingStyle.Delete
{
todoItem.removeAtIndex(indexPath.row)
date.removeAtIndex(indexPath.row)
NSUserDefaults.standardUserDefaults().setObject(todoItem, forKey: "ToDoItem ")
NSUserDefaults.standardUserDefaults().setObject(date, forKey: "ToDoDate")
table.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
if
NSUserDefaults.standardUserDefaults().objectForKey("date") != nil{
date = NSUserDefaults.standardUserDefaults().objectForKey("date") as! [String]
}
if
NSUserDefaults.standardUserDefaults().objectForKey("ToDoDate") != nil{
date = NSUserDefaults.standardUserDefaults().objectForKey("ToDoDate") as! [String]
}
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}