Core data 核心数据&x2B;NSFetchedResultsController在SWIFT中
我将尽可能简单地使用NSFetchedResultsController从核心数据中提取数据。我的cellForRowAtIndexPath如下所示Core data 核心数据&x2B;NSFetchedResultsController在SWIFT中,core-data,swift,nsfetchedresultscontroller,Core Data,Swift,Nsfetchedresultscontroller,我将尽可能简单地使用NSFetchedResultsController从核心数据中提取数据。我的cellForRowAtIndexPath如下所示 override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell? { var cell = tableView.dequeueReusableCellWithIdenti
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell? {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
let newEntry : Entry = fetchedResultController.objectAtIndexPath(indexPath) as Entry
cell.textLabel.text = newEntry.title
return cell
}
我的应用程序崩溃了
当我替换下面的行时
let newEntry : Entry = fetchedResultsController.objectAtIndexPath(indexPath) as Entry
使用此行,应用程序工作记录填充tableview无错误无问题
let newEntry : AnyObject! = self.fetchedResultsController.objectAtIndexPath(indexPath)
问题是为什么AnyObject不是条目NSManagedObject
谢谢NSFetchedResultsController没有问题,但是Objective-C需要识别您的NSManagedObject子类。只需在NSManagedObject子类中添加@objc(条目):
@objc(Entry)
class Entry: NSManagedObject {
@NSManaged var title: String
}
我面临着类似的问题。现在我已经按照最新的iOS版本成功地纠正了它。希望我的代码有帮助
if let newEntry = fetchedResultsController?.objectAtIndexPath(indexPath) as? Entry{
cell.textLabel?.text = newEntry.title
}
return cell
试试这个
if let access:Entry = self.fetchedResultsController.objectAtIndexPath(indexPath) as? Entry {
cell.textLabel?.text = access.valueForKey("title") as? String
}
然后
返回到单元格
建议:两个问题应作为两个独立的问题写入SO。这似乎不起作用。我不认为@ngeran希望在Objective-C中使用该类,添加@objc(Entry)
并不能解决Swift中的问题。它似乎与NSManagedObject Swift名称空间有关。