2014-07-16 174 views
0

我会尽量做到尽可能的简单我是从核心数据使用NSFetchedResultsController我的cellForRowAtIndexPath看起来像这样核心数据+ NSFetchedResultsController在SWIFT

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

谢谢

+0

建议:两个问题应该写为SO两个独立的问题。 – andrewbuilder

回答

3

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

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

这似乎并不奏效。我不认为@ngeran正在使用Objective-C中的类,并且添加'@objc(Entry)'并不能解决Swift中的问题。它似乎与NSManagedObject Swift命名空间有关。 – PLJNS

0

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

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

试试看!

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

然后returncell