1

我一直在按照教程学习 T,但是当我运行我的应用程序时,每次启动时都会失败,并出现以下错误:

*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“+entityForName:nil 不是搜索实体名称“ArticleInfo”的合法 NSManagedObjectContext 参数

但我无法弄清楚我做错了什么会导致这种情况。作为我所做工作的背景,我有一个数据模型文件,其中包含一个名为 ArticleInfo 的实体,该实体具有一堆不同类型的属性(一些临时属性,如果需要注意的话)。按照本文的建议,将其分类NSManagedObjectArticleInfo.

如果值得注意的话,我在数据模型中创建了preview,wordsInBodyprogress瞬态(我在数据模型检查器中给出position了默认值)。0因此,在子类中,我制作了如下自定义 getter:

- (NSString *)preview {
    [self willAccessValueForKey:@"preview"];
    NSString *preview = [self primitiveValueForKey:@"preview"];
    [self didAccessValueForKey:@"preview"];

    if (self.body.length < 200) {
        preview = self.body;
    }
    else {
        preview = [self.body substringWithRange:NSMakeRange(0, 200)];
    }

    return preview;
}

- (NSNumber *)progress {
    [self willAccessValueForKey:@"progress"];
    NSNumber *progress = [self primitiveValueForKey:@"progress"];
    [self didAccessValueForKey:@"progress"];

    if (self.body.length == 0) {
        progress = @100;
    }
    else {
        progress = @(100 * [self.position intValue] / [self.wordsInBody intValue]);
    }

    return progress;
}

- (NSNumber *)wordsInBody {
    [self willAccessValueForKey:@"wordsInBody"];
    NSNumber *wordsInBody = [self primitiveValueForKey:@"wordsInBody"];
    [self didAccessValueForKey:@"wordsInBody"];

    if (!wordsInBody) {
        __block int numberOfWordsInBody = 0;
        NSRange range = {0, self.body.length};
        [self.body enumerateSubstringsInRange:range options:NSStringEnumerationByWords usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
            numberOfWordsInBody++;
        }];

        wordsInBody = @(numberOfWordsInBody);
    }

    return wordsInBody;
}

(同样,不确定这是否相关。)

现在在我的主 viewcontroller 类(具有我正在使用的 tableview 的那个NSFetchedResultsController)中,我声明了该属性,并覆盖了它的 getter:

在.h中:

@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;

以 .m 为单位:

- (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;
}

但同样,它在应用启动时一直给我这个错误。我到底做错了什么导致该错误?

4

1 回答 1

2

NSEntityDescription *entity = [NSEntityDescription entityForName:@"ArticleInfo" inManagedObjectContext:context]

这里的东西是零。记录上下文和实体以查看哪个。也许您模型中的实体名称不是“ArticleInfo”(错字?),或者您的应用程序委托在创建上下文时遇到问题。

于 2013-04-06T20:17:54.380 回答