首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Core数据添加多个测试数据

使用Core数据添加多个测试数据
EN

Stack Overflow用户
提问于 2012-02-17 03:48:13
回答 1查看 292关注 0票数 0

我是一个新手,从零开始学习核心数据。我已经设置了coredatamodel,然后创建了NSManagedObject类。然后,在应用程序委托中,我尝试插入一些测试数据。然而,它没有正常工作。只插入了最后一个数据。我该把

自saveContext;

在每个物体之间?在"applicationWillTerminate“方法中,调用saveContext方法,因此保存了最后一项。(是对的吗?)

代码语言:javascript
复制
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";
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-17 03:59:02

您需要为插入的每个词汇表对象插入一个新实体。所以要这样做:

代码语言:javascript
复制
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";
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9322598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档