1

我已经按照本教程http://www.raywenderlich.com/12170/core-data-tutorial-how-to-preloadimport-existing-data-updated

我现在习惯于教程并创建自己的应用程序,我有两个实体并因为我的实体具有一对多关系而陷入困境。

制作(属性:carMake)模型(属性:carModel)

每个品牌都有许多模型。(一对多关系定义为(模型))。

我也将逆向设置为“make”。

我有两个问题,首先你将如何在 JSON 文件中填充数据,如下所示?以汽车为数据模型名称。

[{ "cars":{ 
           "carMake": "BMW", 
           "models": [ 
                      {"carModel": "1 Series"} 
                      {"carModel": "3 Series"} 
                      { "carModel": "4 Series"}  
                     ] 
           "carMake": "Audi", 
           "models": [ 
                      {"carModel": "A4"} 
                      {"carModel": "A3"} 
                     ] 
         } 
}] 

其次,我如何通过 xcode 将这些数据转换为 sqlite 数据库,就像在 Rays 教程中一样,他没有说明如何为一对多关系执行此操作。

提前感谢您发布回复的任何人。

下面是用于使用 to one 关系将数据从 JSON 文件预填充到 SQLITE 的代码:

[Banks enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    FailedBankInfo *failedBankInfo = [NSEntityDescription
                                  insertNewObjectForEntityForName:@"FailedBankInfo"
                                  inManagedObjectContext:context];
    failedBankInfo.name = [obj objectForKey:@"name"];
    failedBankInfo.city = [obj objectForKey:@"city"];
    failedBankInfo.state = [obj objectForKey:@"state"];
    FailedBankDetails *failedBankDetails = [NSEntityDescription
                                          insertNewObjectForEntityForName:@"FailedBankDetails"
                                        inManagedObjectContext:context];
    failedBankDetails.closeDate = [NSDate dateWithString:[obj objectForKey:@"closeDate"]];
    failedBankDetails.updateDate = [NSDate date];
    failedBankDetails.zip = [obj objectForKey:@"zip"];
    failedBankDetails.info = failedBankInfo;
    failedBankInfo.details = failedBankDetails;
    NSError *error;
    if (![context save:&error]) {
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
    }
}];
4

1 回答 1

0

这就是我这样做的方式:

 NSError* err = nil;
 NSArray *jsonArray = [[NSArray alloc] init];
 NSString* dataPath = [[NSBundle mainBundle] pathForResource:@"Words" ofType:@"json"];
 jsonArray = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath]
                                              options:kNilOptions
                                                error:&err];
NSManagedObjectContext *context = [self managedObjectContext];

[jsonArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        WordEntity *word = [NSEntityDescription
                            insertNewObjectForEntityForName:@"WordEntity"
                            inManagedObjectContext:context];

word.greekText = [obj objectForKey:@"greektext"];
word.englishText = [obj objectForKey:@"englishtext"];
word.uid = [NSString stringWithFormat:@"%@", [obj objectForKey:@"uid"]];
word.category = [obj valueForKey:@"category"];


        NSError *error;
        if (![context save:&error]) {
            NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
        }
    }];
}
于 2014-08-03T07:05:32.837 回答