2014-07-16 172 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