通过JSON接收空条目来填充核心数据

时间:2013-08-14 20:39:27

标签: ios xcode json core-data

尝试使用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数据库之后,所有属性都用空值填充。

   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]);
        }
    }];

为第二个实体编辑了上面的代码,但未能说明数据参数是否为

1 个答案:

答案 0 :(得分:0)

您正在枚举从放入Exercises的磁盘上的JSON数据加载的每个对象(顺便说一句,不要将局部变量的名称大写)但是您没有使用这些对象作为数据源,而是使用jsonDict,它是从字符串硬编码的,只有一个键/值对。尝试更改行

id value = [jsonDict objectForKey:attribute];

改为说:

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

这应该实际应用您加载的数据。但是,您的代码中还存在其他问题 - 您只插入一个实体,而不是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]);
    }
}];