私は Core Data プログラミングと Cocoa 全般にかなり慣れていないので、問題を抱えているのも不思議ではありません :)
だからここに私のmanagedObjectModelメソッドがあります:
- (NSManagedObjectModel *)managedObjectModel
{
if (managedObjectModel != nil)
{
return managedObjectModel;
}
NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"Model" ofType:@"momd"];
NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
NSAssert(modelURL != nil,@"modelURL == nil");
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return managedObjectModel;
}
クラッシュするコードの部分は次のとおりです。
NSManagedObjectModel *mom = [self managedObjectModel];
managedObjectModel = mom;
if (applicationLogDirectory() == nil)
{
NSLog(@"Could not find application logs directory\nExiting...");
exit(1);
}
NSManagedObjectContext *moc = [self managedObjectContext];
NSProcessInfo *processInfo = [NSProcessInfo processInfo];
NSEntityDescription *newShotEntity = [[mom entitiesByName] objectForKey:@"Entity"];
Entity *shEnt = [[Entity alloc] initWithEntity:newShotEntity insertIntoManagedObjectContext:moc];
shEnt.pid = [processInfo processIdentifier]; // EXC_BAD_ACCESS (code=1, address=0x28ae) here !!!
NSError *error;
if (![moc save: &error])
{
NSLog(@"Error while saving\n%@",
([error localizedDescription] != nil) ? [error localizedDescription] : @"Unknown Error");
exit(1);
}
.xcdatamodeld ファイルを使用する代わりにデータ モデルをハードコードしたとき、問題なく動作していたので、なぜこのエラーが発生するのか本当に混乱しています。
どんな種類の助けも本当に感謝しています!
編集 1: これらすべての質問が寄せられているので、すべてを明確にしたいと思います。以前にこれらすべてを提供できなかったことをお詫びします。
// Entity.h
#import <CoreData/CoreData.h>
@interface Entity : NSManagedObject
@property (strong) NSDate *date;
@property (assign) NSInteger pid;
@end
//Entity.m
#import "Entity.h"
@interface Entity ()
@property (strong) NSDate *primitiveDate;
@end
@implementation Entity
@dynamic date,primitiveDate,pid;
- (void) awakeFromInsert
{
[super awakeFromInsert];
self.primitiveDate = [NSDate date];
}
- (void)setNilValueForKey:(NSString *)key
{
if ([key isEqualToString:@"pid"]) {
self.pid = 0;
}
else {
[super setNilValueForKey:key];
}
}
@end