0

我有以下代码:

- (NSFetchedResultsController *)fetchedResultsController {
    if (!_fetchedResultsController) {
        NSManagedObjectContext *context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"ArticleInfo" inManagedObjectContext:context];
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        request.entity = entity;

        NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
        request.sortDescriptors = [NSArray arrayWithObject:descriptor];

        request.fetchBatchSize = 20;

        NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:nil cacheName:@"Root"];
        _fetchedResultsController = fetchedResultsController;
        _fetchedResultsController.delegate = self;
    }

    return _fetchedResultsController;
}

它报告此错误:

从不兼容的类型'RootViewController *const __strong'分配给'id'

我到底做错了什么导致这种情况?

4

1 回答 1

1

您需要让编译器知道您的类符合NSFetchedResultsControllerDelegate协议。

您可以通过将其添加到.h文件中来执行此操作

@interface RootViewController : UITableViewController <NSFetchedResultsControllerDelegate>

或者可能更优选将其添加到类扩展中。所以在你的.m文件中

@interface RootViewController () <NSFetchedResultsControllerDelegate>

这可以被认为更好一点的原因是它更好地隐藏信息 - 没有其他类需要知道RootViewController符合NSFetchedResultsControllerDelegate协议的,那么为什么要让它公开可见呢?

于 2013-04-05T22:03:04.200 回答