【发布时间】:2015-07-20 22:35:59
【问题描述】:
我正在尝试从 TableView 和名为“Books”的实体中删除项目。但是,我不知道我是否在正确的轨道上。当我尝试这段代码时:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
var appdel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var context:NSManagedObjectContext = appdel.managedObjectContext!
var request = NSFetchRequest(entityName: "Books")
if editingStyle == UITableViewCellEditingStyle.Delete {
addBook.myBooks.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
context.deleteObject(addBook.myBooks[indexPath.row] as! NSManagedObject)
}
}
我在最后一行收到一条警告,上面写着“从 'String' 转换为不相关类型 'NSManagedObject' 总是失败。”有谁知道我该如何解决这个问题?我读过您可以使用 fetchedResultsController 轻松处理表中的核心数据,但我是编程新手,发现在设置核心数据时该方法更容易混淆。 fetchedResultsController 是管理我的 TableView 中的数据所必需的吗?
【问题讨论】: