Iphone 获取NSInternalInconsistencyException错误!
我得到一个错误: ***由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“+entityForName:找不到实体名称“练习”的NSManagedObjectModel” 在以下行:Iphone 获取NSInternalInconsistencyException错误!,iphone,objective-c,Iphone,Objective C,我得到一个错误: ***由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“+entityForName:找不到实体名称“练习”的NSManagedObjectModel” 在以下行:Exercise*Exercise=(Exercise*)[NSEntityDescription insertNewObjectForEntityForName:@“Exercise”inManagedObjectContext:managedObjectC
Exercise*Exercise=(Exercise*)[NSEntityDescription insertNewObjectForEntityForName:@“Exercise”inManagedObjectContext:managedObjectContext]代码>
(您可以在此处看到我的数据模型:)。
你知道为什么吗
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(exerciseChooser)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
//if (managedObjectContext == nil)
{
managedObjectContext = [(CurlAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Exercise" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSLog(@"After managedObjectContext: %@", managedObjectContext);
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
// Handle the error.
}
[mutableFetchResults release];
[request release];
}
-(IBAction)exerciseChooser
{
RoutineExerciseChooserViewController *routineExerciseChooserViewController = [[[RoutineExerciseChooserViewController alloc] init] autorelease];
[self.navigationController pushViewController:routineExerciseChooserViewController animated:YES];
}
-(void)addExercise
{
Exercise *exercise = (Exercise *)[NSEntityDescription insertNewObjectForEntityForName:@"Exercise" inManagedObjectContext:managedObjectContext];
exercise.name=@"Test";
NSError *error = nil;
if (![managedObjectContext save:&error])
{
// Handle the error.
}
NSLog(@"%@", error);
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSInteger lastSection = [self.tableView numberOfSections] -1;
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.tableView numberOfRowsInSection:lastSection]-1 inSection:lastSection] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
此错误只有几个可能的来源:
实体名称中的输入错误
无托管对象上下文对象
未能将包含实体的模型添加到上下文使用的持久存储中
未能将正确的持久存储添加到上下文本身
另请参阅上一个问题:
更新
为什么if
语句被注释掉了
if(managedObjectContext == nil)
我想这是必须的
编辑
-(void)addExercise
{
if(managedObjectContext!=nil)
{
Exercise *exercise = (Exercise *)[NSEntityDescription insertNewObjectForEntityForName:@"Exercise" inManagedObjectContext:managedObjectContext];
exercise.name=@"Test";
NSError *error = nil;
if (![managedObjectContext save:&error])
{
// Handle the error.
}
NSLog(@"%@", error);
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSInteger lastSection = [self.tableView numberOfSections] -1;
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.tableView numberOfRowsInSection:lastSection]-1 inSection:lastSection] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
}
我把它注释掉了,因为当它没有注释时,它不会创建托管对象,我知道这一点,因为我的NSLOG不会打印,我不知道。我只是把它们留在这里,以防万一我取消了if语句的注释,但是错误是在问题中指出的行中以SIGABRT的形式出现的。你检查过objectmodel是否存在吗?在添加新项之前检查它,我认为这是因为您的objectmodel为nil。