我是一个新手,从零开始学习核心数据。我已经设置了coredatamodel,然后创建了NSManagedObject类。然后,在应用程序委托中,我尝试插入一些测试数据。然而,它没有正常工作。只插入了最后一个数据。我该把
自saveContext;
在每个物体之间?在"applicationWillTerminate“方法中,调用saveContext方法,因此保存了最后一项。(是对的吗?)
NSManagedObjectContext *context = [self managedObjectContext];
Vocabulary *vocabulary = [NSEntityDescription
insertNewObjectForEntityForName:@"Vocabulary"
inManagedObjectContext:context];
vocabulary.word = @"iPhone";
vocabulary.definition = @"better than Android";
vocabulary.level = @"beginner";
vocabulary.word = @"iPhone3gs";
vocabulary.definition = @"better than 3";
vocabulary.level = @"intermediate";
vocabulary.word = @"iPhone4";
vocabulary.definition = @"better than 3gs";
vocabulary.level = @"advanced";
vocabulary.word = @"iPhone4s";
vocabulary.definition = @"better than 4";
vocabulary.word = @"iPhone4s";
vocabulary.definition = @"64 is better than 32";
vocabulary.level = @"advanced";发布于 2012-02-17 03:59:02
您需要为插入的每个词汇表对象插入一个新实体。所以要这样做:
Vocabulary *vocabulary = nil;
NSManagedObjectContext *context = [self managedObjectContext];
vocabulary = [NSEntityDescription insertNewObjectForEntityForName:@"Vocabulary" inManagedObjectContext:context];
vocabulary.word = @"iPhone";
vocabulary.definition = @"better than Android";
vocabulary.level = @"beginner";
vocabulary = [NSEntityDescription insertNewObjectForEntityForName:@"Vocabulary" inManagedObjectContext:context];
vocabulary.word = @"iPhone3gs";
vocabulary.definition = @"better than 3";
vocabulary.level = @"intermediate";
vocabulary = [NSEntityDescription insertNewObjectForEntityForName:@"Vocabulary" inManagedObjectContext:context];
vocabulary.word = @"iPhone4";
vocabulary.definition = @"better than 3gs";
vocabulary.level = @"advanced";
vocabulary = [NSEntityDescription insertNewObjectForEntityForName:@"Vocabulary" inManagedObjectContext:context];
vocabulary.word = @"iPhone4s";
vocabulary.definition = @"better than 4";
vocabulary = [NSEntityDescription insertNewObjectForEntityForName:@"Vocabulary" inManagedObjectContext:context];
vocabulary.word = @"iPhone4s";
vocabulary.definition = @"64 is better than 32";
vocabulary.level = @"advanced";https://stackoverflow.com/questions/9322598
复制相似问题