我在处理我的 Core Data 时遇到问题NSManagedObjectContext
。
我可以NSManagedObject
在我的中创建NSManagedObjectContext
,但我未能保存该值。
这是我得到的:
_lesson.title = _titleField.text;
int priority = [_priorityField.text intValue];
int difficulty = [_difficultyField.text intValue];
int time = [_timeField.text intValue];
int sortIndex = 0;
if ( time == 0 )
{
sortIndex = 101;
}
else
{
sortIndex = priority * ( difficulty / time );
}
_lesson.priority = [NSNumber numberWithInt:priority];
_lesson.difficulty = [NSNumber numberWithInt:difficulty];
_lesson.time = [NSNumber numberWithInt:time];
_lesson.sortIndex = [NSNumber numberWithInt:sortIndex];
NSError* error = nil;
[[(AppDelegate*)[[UIApplication sharedApplication] delegate] managedObjectContext] save:&error];
保存之前的一切工作正常,我曾经NSLog
验证每个值是否真的保存在_lesson
.
并_lesson
从这里发送:
if ( [[segue identifier] isEqualToString:@"addLesson"] )
{
LessonViewController* destination = [[LessonViewController alloc]init];
Lesson* lesson = (Lesson*)[NSEntityDescription insertNewObjectForEntityForName:@"Lesson" inManagedObjectContext:_managedObjectContext];
destination.lesson = lesson;
}
else if ( [[segue identifier] isEqualToString:@"editLesson"] )
{
LessonViewController* destination = [[LessonViewController alloc]init];
NSIndexPath* index = [_tableView indexPathForCell:(UITableViewCell*)sender];
[_managedObjectContext deleteObject:[_lessonArray objectAtIndex:index.row]];
Lesson* lesson = (Lesson*)[_lessonArray objectAtIndex:index.row];
destination.lesson = lesson;
}
调试两个小时后,我找不到我的错误。请帮忙!
我将在下面包含我的完整代码:
https://www.dropbox.com/sh/eu62ie9svbbqdmm/u1hYUICfjy
那是我的完整源代码。(我复制并粘贴并弄得一团糟。所以,Dropbox!)
提前致谢。