核心数据+ NSFetchedResultsController在SWIFT [英] Core Data + NSFetchedResultsController in SWIFT

查看:196
本文介绍了核心数据+ NSFetchedResultsController在SWIFT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会尽量使它尽可能简单,我使用NSFetchedResultsController从核心数据获取我的cellForRowAtIndexPath看起来像这样

I will try to make it as simple as possible i am fetching from core data using NSFetchedResultsController my cellForRowAtIndexPath looks like this

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没有错误没有问题

With this line the app works records populate tableview no errors no problems

let newEntry : AnyObject! = self.fetchedResultsController.objectAtIndexPath(indexPath)

问题是WHY不是AnyObject是一个Entry NSManagedObject

The question is WHY is not AnyObject is an Entry NSManagedObject

谢谢

推荐答案

NSFetchedResultsController没有问题, -C需要识别你的NSManagedObject子类。只需在NSManagedObject子类中添加@objc(Entry):

There is no problem with NSFetchedResultsController, but Objective-C needs to recognize your NSManagedObject subclass. Just add the @objc(Entry) in your NSManagedObject subclass:

@objc(Entry)
class Entry: NSManagedObject {
    @NSManaged var title: String
}

这篇关于核心数据+ NSFetchedResultsController在SWIFT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆