このチュートリアルに従ってT を実行しましたが、アプリを実行すると、毎回次のエラーで起動に失敗します。
*キャッチされない例外 'NSInvalidArgumentException' が原因でアプリを終了します。理由: '+entityForName: nil は、エンティティ名 'ArticleInfo' を検索する正当な NSManagedObjectContext パラメータではありません
しかし、私はそれを引き起こす原因となる間違ったことを理解できません。私が行ったことの背景として、さまざまなタイプの属性の束を含む ArticleInfo というエンティティを含むデータモデル ファイルがあります (注意が必要な場合は一時的なものもあります)。この記事のアドバイスに従って、私はNSManagedObject
としてサブクラス化しましArticleInfo
た。
注目に値する場合は、データモデルでpreview
,wordsInBody
とprogress
トランジェントを作成しました (また、Data Model Inspector でposition
デフォルト値を指定しました)。0
したがって、サブクラスでは、次のようにカスタムゲッターを作成しました。
- (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;
}
(繰り返しますが、それが関連しているかどうかはわかりません。)
ここで、メインのビューコントローラー クラス (使用しているテーブルビューを持つクラスNSFetchedResultsController
) で、プロパティを宣言し、そのゲッターをオーバーライドしました。
.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;
}
しかし、繰り返しになりますが、アプリの起動時にそのエラーが発生し続けます。そのエラーの原因は何ですか?