通过具有多个实体的JSON填充核心数据模型

时间:2013-08-14 22:08:51

标签: ios xcode json core-data

通过JSON文件填充带有两个实体的核心数据模型的问题

我设法填充了第一个没有问题的实体,但是一旦我尝试填充第二个实体,就收到一个错误,说明数据参数是nil,

我检查过我使用了正确的json文件名(Weights.json)。

以下是代码:

 // Create the managed object context
    NSManagedObjectContext *context = managedObjectContext();
    // Save the managed object context
    NSError *error = nil;
    if (![context save:&error]) {
        NSLog(@"Error while saving %@", ([error localizedDescription] != nil) ? [error localizedDescription] : @"Unknown Error");
        exit(1);
    }

    NSError* err = nil;
    NSString* dataPath = [[NSBundle mainBundle] pathForResource:@"Exercises" ofType:@"json"];
    NSArray* exercisesFromJSON = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath]
                                                                 options:kNilOptions
                                                                   error:&err];
    //NSLog(@"Imported exercisesFromJSON: %@", exercisesFromJSON);


    [exercisesFromJSON enumerateObjectsUsingBlock:^(NSDictionary *exerciseDictionary, NSUInteger idx, BOOL *stop) {

        Exercise *exercise = [NSEntityDescription insertNewObjectForEntityForName:@"Exercise"
                                                           inManagedObjectContext:context];
        NSDictionary *attributes = [[exercise entity] attributesByName];
        for (NSString *attribute in [attributes allKeys]) {
            id value = [exerciseDictionary objectForKey:attribute];
            if (value == nil) {
                continue;
            }
            [exercise setValue:value forKey:attribute];
        }
        NSError *error;
        if (![context save:&error]) {
            NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
        }
    }];

    NSString* secondDataPath = [[NSBundle mainBundle] pathForResource:@"Weights" ofType:@"json"];
    NSArray* weightsFromJSON = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:secondDataPath]
                                                                 options:kNilOptions
                                                                   error:&err];


  ///***Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'****//////
    NSLog(@"Imported weightsFromJSON: %@", weightsFromJSON);


    [weightsFromJSON enumerateObjectsUsingBlock:^(NSDictionary *weightDictionary, NSUInteger idx, BOOL *stop) {

        Weight *weight = [NSEntityDescription insertNewObjectForEntityForName:@"Weight"
                                                           inManagedObjectContext:context];
        NSDictionary *attributes = [[weight entity] attributesByName];
        for (NSString *attribute in [attributes allKeys]) {
            id value = [weightDictionary objectForKey:attribute];
            if (value == nil) {
                continue;
            }
            [weight setValue:value forKey:attribute];
        }
        NSError *error;
        if (![context save:&error]) {
            NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
        }
    }];

提前感谢您的帮助。

0 个答案:

没有答案