我创建了一个名为“Athlete”的核心数据实体。
这是我得到的错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Athlete''
这是它中断的地方:
Athlete *detail = [NSEntityDescription insertNewObjectForEntityForName:@"Athlete" inManagedObjectContext:context];
委托人.h
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
委托人.m
-(void)createData{
NSManagedObjectContext *context = [self managedObjectContext];
Athlete *detail = [NSEntityDescription insertNewObjectForEntityForName:@"Athlete" inManagedObjectContext:context];
detail.first = @"Joe";
detail.last = @"Pastrami";
detail.phone = @"(123)456-7891";
NSError *error;
if(![context save:&error]){
NSLog(@"Error :(");
}
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Athlete" inManagedObjectContext:context];
[request setEntity:entity];
NSArray *arr = [context executeFetchRequest:request error:&error];
for (Athlete *ath in arr){
NSLog(@"Name %@", ath.first);
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self createData];
}