【问题标题】:Core Data + NSFetchedResultsController in SWIFTSWIFT 中的核心数据 + NSFetchedResultsController
【发布时间】:2014-07-16 14:50:24
【问题描述】:

我将尝试使其尽可能简单,我使用 NSFetchedResultsController 从核心数据中获取我的 cellForRowAtIndexPath 看起来像这样

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 不是 Entry NSManagedObject

谢谢

【问题讨论】:

  • 建议:两个问题应该在 SO 中写成两个单独的问题。

标签: core-data swift nsfetchedresultscontroller


【解决方案1】:

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

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

【讨论】:

  • 这似乎不起作用。我不认为@ngeran 想要在 Objective-C 中使用该类,并且添加 @objc(Entry) 并不能解决我在 Swift 中的问题。它似乎与 NSManagedObject Swift 命名空间有关。
【解决方案2】:

我遇到了类似的问题。现在我已经按照最新的 iOS 版本成功纠正了它。希望我的代码有所帮助。

if let newEntry = fetchedResultsController?.objectAtIndexPath(indexPath) as? Entry{
   cell.textLabel?.text = newEntry.title 
}
return cell

【讨论】:

    【解决方案3】:

    试试这个!

    if let access:Entry = self.fetchedResultsController.objectAtIndexPath(indexPath) as? Entry {
        cell.textLabel?.text = access.valueForKey("title") as? String
    }
    

    然后returncell

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多