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 数据库后,所有属性都填充为空值。

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

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

4

1 回答 1

0

您正在枚举从您放入的磁盘上的 JSON 数据加载的每个对象(顺便说Exercises一句,不要将局部变量的名称大写),但是您没有使用这些对象作为数据源,而是使用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]);
    }
}];
于 2013-08-14T20:52:36.480 回答