如何在Swift中编写NSFetchedResultsController

时间:2014-06-11 12:54:44

标签: objective-c swift xcode6

无法弄清楚如何在 swift 中重写以下方法 为了更好的学习曲线,我试着翻译这段代码。所以让我们选择一个更难的方法来做到这一点。它有error handlingobject initparameter settingsarray of object pointersselectorsnilself ..

目标-C:

-(NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    [fetchRequest setFetchBatchSize:20];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
    NSArray *sortDescriptors = @[sortDescriptor];
    [fetchRequest setSortDescriptors:sortDescriptors];

    NSFetchedResultsController *frC = [[NSFetchedResultsController alloc]
        initWithFetchRequest:fetchRequest
        managedObjectContext:self.managedObjectContext 
        sectionNameKeyPath:nil 
        cacheName:@"Master"];
    frC.delegate = self;
    self.fetchedResultsController = frC;

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _fetchedResultsController;
}

特别无法弄清楚如何致电NSEntityDescription *entity。 在解释规范时,我无法重写这个正确的..

迅速:

var fetchedResultsController: NSFetchedResultsController {

    var _fetchedResultsController: NSFetchedResultsController? = nil  //??

    var fetchRequest: NSFetchRequest? = nil  // ??
    var entity: NSEntityDescription = NSEntityDescription( /*. . .*/ ) //??

    fetchRequest.entity(entity) //?? sure this is wrong

    ...

    return _fetchedResultsController
}

2 个答案:

答案 0 :(得分:6)

这看起来就像使用CoreData设置主详细信息项目时获得的代码。

你可以在Xcode 6中做同样的事情,但选择Swift作为语言。在MasterViewController中,您得到:

var fetchedResultsController: NSFetchedResultsController {
    if _fetchedResultsController != nil {
        return _fetchedResultsController!
    }

    let fetchRequest = NSFetchRequest()
    // Edit the entity name as appropriate.
    let entity = NSEntityDescription.entityForName("Event", inManagedObjectContext: self.managedObjectContext)
    fetchRequest.entity = entity

    // Set the batch size to a suitable number.
    fetchRequest.fetchBatchSize = 20

    // Edit the sort key as appropriate.
    let sortDescriptor = NSSortDescriptor(key: "timeStamp", ascending: false)
    let sortDescriptors = [sortDescriptor]

    fetchRequest.sortDescriptors = [sortDescriptor]

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: nil, cacheName: "Master")
    aFetchedResultsController.delegate = self
    _fetchedResultsController = aFetchedResultsController

    var error: NSError? = nil
    if !_fetchedResultsController!.performFetch(&error) {
         // Replace this implementation with code to handle the error appropriately.
         // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
         //println("Unresolved error \(error), \(error.userInfo)")
         abort()
    }

    return _fetchedResultsController!
}    
var _fetchedResultsController: NSFetchedResultsController? = nil

这看起来像一个行到线的翻译,我希望它有所帮助。

答案 1 :(得分:5)

试试懒惰:

lazy var fetchedResultsController: NSFetchedResultsController = {
    let fetchRequest = NSFetchRequest()
    // Edit the entity name as appropriate.
    let entity = NSEntityDescription.entityForName("Event", inManagedObjectContext: self.managedObjectContext())
    fetchRequest.entity = entity

    // Set the batch size to a suitable number.
    fetchRequest.fetchBatchSize = 20

    // Edit the sort key as appropriate.
    let sortDescriptor = NSSortDescriptor(key: "timeStamp", ascending: false)
    let sortDescriptors = [sortDescriptor]

    fetchRequest.sortDescriptors = [sortDescriptor]

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext(), sectionNameKeyPath: nil, cacheName: "Master")
    aFetchedResultsController.delegate = self

    var error: NSError? = nil
    if !aFetchedResultsController.performFetch(&error) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        //println("Unresolved error \(error), \(error.userInfo)")
        abort()
    }

    return aFetchedResultsController
}()