1

JSONを使用してコアデータ構造にデータを入力しようとすると、

コードは以下に記載されていますか?

    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* Exercises = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath]
                                                         options:kNilOptions
                                                          error:&err];
    NSLog(@"Imported Exercises: %@", Exercises);


    NSManagedObject *object = [NSEntityDescription insertNewObjectForEntityForName:@"Exercise" inManagedObjectContext:context];
    NSString *theJSONString = @"{\"key\":\"value\"}";
    NSError *theError = NULL;
    NSDictionary *jsonDict = [NSDictionary dictionaryWithJSONString:theJSONString error:&theError];

    Exercise *exercise = [NSEntityDescription insertNewObjectForEntityForName:@"Exercise"
                                                       inManagedObjectContext:context];

    [Exercises enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

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

コードがコンパイルされ、作成された sqlite データベースを分析した後、すべての属性が null 値で埋められます。

   NSString* secondDataPath = [[NSBundle mainBundle] pathForResource:@"Weights" ofType:@"json"];
    NSArray* weightsFromJSON = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:secondDataPath]
                                                                 options:kNilOptions
                                                                   error:&err];
    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]);
        }
    }];

上記のコードを 2 番目のエンティティ用に編集しましたが、データ パラメータが Hill であるという記述に失敗しました

4

1 に答える 1

0

入れたディスク上のJSONデータからロードされたすべてのオブジェクトを列挙していますExercises(余談ですが、ローカル変数の名前を大文字にしないでください)が、それらのオブジェクトをデータソースとして使用しておらず、代わりに使用していますjsonDictこれは文字列からハードコードされ、キーと値のペアが 1 つしかありません。ラインを変えてみる

id value = [jsonDict objectForKey:attribute];

代わりに次のように言います。

id value = [(NSDictionary *)obj objectForKey:attribute];

これにより、ロードしたデータが実際に適用されるはずです。ただし、コードには他にも問題があります。 の各要素に対して 1 つではなく、単一のエンティティのみを挿入するだけでなく、Exercisesに割り当てる余分なエンティティを挿入しますobject。あなたがやろうとしていることをするかもしれないと私が思ういくつかのコードはここにあります:

NSError* err = nil;
NSString* dataPath = [[NSBundle mainBundle] pathForResource:@"exercisesFromJSON" 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]);
    }
}];
于 2013-08-14T20:52:36.480 に答える