2019-08-18 08:27:21.189525 + 0700 CoreDataTodoList [912:22298] [错误]错误: CoreData:错误:无法在上调用指定的初始化程序 NSManagedObject类'CoreDataTodoList.Item' CoreData:错误:CoreData:错误:无法调用指定的初始化程序 NSManagedObject类'CoreDataTodoList.Item'
2019-08-18 08:27:21.189667 + 0700 CoreDataTodoList [912:22298]- [CoreDataTodoList.Item setTitle:]:无法识别的选择器已发送到实例0x600002395280 2019-08-18 08:27:21.192830 + 0700 CoreDataTodoList [912:22298] * 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序, 原因:'-[CoreDataTodoList.Item setTitle:]:无法识别的选择器已发送到实例0x600002395280' * 首先抛出调用堆栈:
import UIKit
import CoreData
class TodoListTableViewController: UITableViewController,UISearchBarDelegate {
@IBOutlet var mTableView: UITableView!
@IBOutlet weak var mSearchBar: UISearchBar!
@IBOutlet weak var mAddItem: UIBarButtonItem!
var dataList = [Item]()
let dataFilePath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("Items.plist")
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
override func viewDidLoad() {
super.viewDidLoad()
print("----->", dataFilePath)
let newItem = Item()
newItem.title = "Car Book Catalog"
dataList.append(newItem)
let newItem2 = Item()
newItem2.title = "Mobile Book Catalog"
dataList.append(newItem2)
let newItem3 = Item()
newItem3.title = "Programming Book Catalog"
dataList.append(newItem3)
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return dataList.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("indexpath.row call for row at")
print("....")
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseCell", for: indexPath)
let item = dataList[indexPath.row]
cell.textLabel?.text = item.title
cell.accessoryType = item.done ? .checkmark : .none
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
dataList[indexPath.row].done = !dataList[indexPath.row].done
self.saveItem()
tableView.deselectRow(at: indexPath, animated: true)
}
@IBAction func btnAddItem(_ sender: UIBarButtonItem) {
var mAlertTextField = UITextField()
let alert = UIAlertController(title: "Add new todo list", message: "input your info", preferredStyle: .alert)
let actionCancel = UIAlertAction(title: "Calcel", style: .cancel, handler: nil)
let action = UIAlertAction(title: "Add", style: .default) { (action) in
newItem.done = false
newItem.title = mAlertTextField.text!
self.dataList.append(newItem)
self.saveItem()
print(mAlertTextField.text)
}
alert.addTextField { (alertTextField) in
alertTextField.placeholder = "Create a new todo list here"
mAlertTextField = alertTextField
}
alert.addAction(action)
alert.addAction(actionCancel)
present(alert, animated: true, completion: nil)
}
func saveItem() {
do{
try context.save()
}catch {
print("error saving context \(error)")
}
mTableView.reloadData()
}
}