我想在 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)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)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]);
}
.......
如何插入多个数据,50 个问题及其 mcqs(50 个问题对象)? 有没有使用其他东西的手动方式?
MagicalRecord CoreData 的包装器提供了一些不错的导入功能。您需要在包中包含需要作为 plist/json 导入的数据。
在初始运行时,您将数据导入 coreData。
您可以在下面的链接Importing data made easy 中找到一个很好的教程。
你需要创建 NSManagedObject 的子类
您可以在它们上调用 importFromObject:
或 importFromObject:
。如果您对数据的结构有完全的控制权,这将发生在开箱即用的情况下,而无需编写用于将存储的数据映射到核心数据实体的单个代码。
关于ios - 在 CoreData 表中插入 50 个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16917828/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |