无法调用NSFetchedResultsControllerDelegate方法

时间:2014-06-09 13:52:41

标签: ios nsfetchedresultscontroller swift

我已设法使用swift创建我的UICollectionViewController,并实施NSFetchedResultsController并从我的CoreData模型中获取内容。一切都很好......但...... 我似乎无法在我的扩展程序中调用任何NSFetchedResultsControllerDelegate方法。代码如下:

extension MainsViewController: NSFetchedResultsControllerDelegate {

    func fetchedResultsController() -> NSFetchedResultsController {

    //        if(fetchedResultsController != nil){
    //            return fetchedResultsController()
    //        }
    var fetchRequest = NSFetchRequest()

    var context: NSManagedObjectContext = RKObjectManager.sharedManager().managedObjectStore.mainQueueManagedObjectContext

    var entity = NSEntityDescription.entityForName("Dessert", inManagedObjectContext: context)

    fetchRequest.entity = entity
    fetchRequest.fetchBatchSize = 20

    var sortDescriptor = NSSortDescriptor(key: "createdAt", ascending: false)
    let sortDescriptors = [sortDescriptor]
    fetchRequest.sortDescriptors = sortDescriptors
    fetchedResultsController().delegate = self

    var aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: "Master")

    var error: NSError?
    if !aFetchedResultsController.performFetch(&error){
        println("An error:\(error) occured.")
    }



    return aFetchedResultsController
}

//Here is where i would call *@optional func controllerWillChangeContent(_ controller: NSFetchedResultsController!)* etc. etc.

}

但斯威夫特并不认识这些方法。我无法弄清楚我错过了什么。

如果有人可以帮助我,那会很棒。

提前致谢

克里斯

2 个答案:

答案 0 :(得分:0)

我认为它应该是这样的:

var fetchedResultsController: NSFetchedResultsController {
        get {
            var fetchRequest = NSFetchRequest()

            var context: NSManagedObjectContext = RKObjectManager.sharedManager().managedObjectStore.mainQueueManagedObjectContext

            var entity = NSEntityDescription.entityForName("Dessert", inManagedObjectContext: context)

            fetchRequest.entity = entity
            fetchRequest.fetchBatchSize = 20

            var sortDescriptor = NSSortDescriptor(key: "createdAt", ascending: false)
            let sortDescriptors = [sortDescriptor]
            fetchRequest.sortDescriptors = sortDescriptors
            self.fetchedResultsController.delegate = self

            var aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: "Master")

            var error: NSError?
            if !aFetchedResultsController.performFetch(&error){
                println("An error:\(error) occured.")
            }
            return aFetchedResultsController
        }
    }

所以你的MainsViewController是一个类,正如我在上面的评论中所说,所以第一行必须如下所示:

class MainsViewController: UIViewController, NSFetchedResultsControllerDelegate {

不幸的是我无法测试代码,但这可以理解为:你有一个名为fetchedResultsController的属性,当fetchedResultsController获取时,它将执行。所以在getter中就像你在objective-c中用懒惰的获取方法一样。

答案 1 :(得分:0)

我发现这只是自动完成功能,并没有向我提出这种方法。手动输入“固定”问题。很抱歉浪费每个人的时间......

克里斯