1

通过 JSON 文件使用两个实体填充核心数据模型时遇到问题,

我设法毫无问题地填充第一个实体,但是一旦我尝试填充第二个实体,就会收到一个错误,指出数据参数为零,

我检查过我使用了正确的 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]);
        }
    }];

预先感谢您的任何帮助。

4

0 回答 0