我想在coredata ta
中插入50个值 Question.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface Question : NSManagedObject
@property (nonatomic, retain) NSString * question;
@property (nonatomic, retain) NSString * answer;
@property (nonatomic, retain) NSString * mcqsa;
@property (nonatomic, retain) NSString * mcqsb;
@property (nonatomic, retain) NSString * mcqsc;
@property (nonatomic, retain) NSString * mcqsd;
Question.m
#import "Question.h"
@implementation Question
@dynamic question;
@dynamic answer;
@dynamic mcqsa;
@dynamic mcqsb;
@dynamic mcqsc;
@dynamic mcqsd;
@end
插入代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSManagedObjectContext *context = [self managedObjectContext];
Question *question = [NSEntityDescription
insertNewObjectForEntityForName:@"Question"
inManagedObjectContext:context];
question.question = @"What is capital of Australia?";
question.answer = @"Testville";
question.mcqsa = @"Sydney";
question.mcqsb = @"Canbera";
question.mcqsc = @"Berlin";
question.mcqsd = @"Bern";
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
.......
如何使用mcqs插入多个数据,50个问题(50个问题对象)? 是否有任何手动方式使用别的东西?
答案 0 :(得分:1)
MagicalRecord CoreData的包装器提供了一些很好的导入功能。您需要在bundle中包含作为plist / json导入的数据。
在初始运行时,您将数据导入coreData。
您可以在以下链接Importing data made easy
中找到相同的精彩教程您需要创建NSManagedObject的子类
您可以致电importFromObject:
或importFromObject:
。如果您可以完全控制数据的结构,那么这将是开箱即用的,而无需编写单个代码来映射存储到核心数据实体的数据。
答案 1 :(得分:0)
您需要种子存储,预填充核心数据数据库,然后从设备或模拟器文档目录中获取它,然后将其添加到您的包中。首次运行时,您可以将数据库从bundle复制到Document目录,然后使用该数据库
答案 2 :(得分:0)
您可以使用for循环插入数据。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
for(i=0; i<50; i++) {
NSManagedObjectContext *context = [self managedObjectContext];
Question *question = [NSEntityDescription
insertNewObjectForEntityForName:@"Question"
inManagedObjectContext:context];
question.question = @"What is capital of Australia?";
question.answer = @"Testville";
question.mcqsa = @"Sydney";
question.mcqsb = @"Canbera";
question.mcqsc = @"Berlin";
question.mcqsd = @"Bern";
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
.......
}
答案 3 :(得分:0)
以下是代码:
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"QuesList" ofType:@"plist"];
NSArray *quesList = [[NSArray alloc]initWithContentsOfFile:plistPath];
for(NSDictionary *object in quesList){
NSString *str1 = [object objectForKey:@"question"];
NSString *str2 = [object objectForKey:@"answer"];
NSString *str3 = [object objectForKey:@"mcqsa"];
NSString *str4 = [object objectForKey:@"mcqsb"];
NSString *str5 = [object objectForKey:@"mcqsc"];
Question *question = [NSEntityDescription
insertNewObjectForEntityForName:@"Question"
inManagedObjectContext:context];
question.question = str1;
question.answer =str2;
question.mcqsa =str3;
question.mcqsb =str4;
question.mcqsc = str5;
}
并且
还要确保只运行一次此代码,否则它会创建重复的对象