1

JSON ファイルを介してコア データ モデルに 2 つのエンティティを設定する際に問題が発生し、

最初のエンティティには問題なく入力できましたが、2 番目のエンティティに入力しようとすると、データ パラメータが 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]);
        }
    }];

助けてくれてありがとう。

4

0 に答える 0