2

所以我试图用看起来像这样的对象映射 JSON

{       
    "item_id": "0",
    "product_id": "217",
    "color_id": "55",
    "size_id": "37",        
    "images": [
        {
            "image_url": "http://www.foo.com/bar.jpg"
        }
    ]
},

我将它直接映射到核心数据实体“项目”。该实体与称为“图像”的实体“图片”具有一对多的关系。实体“图片”有一个 NSString 属性“image_url”。这是我的映射:

- (void) setupPicturesMapping
{
    picturesMapping = [RKEntityMapping mappingForEntityForName:@"Pictures" inManagedObjectStore:self.objectManager.managedObjectStore];

    picturesMapping.identificationAttributes = @[@"image_url"] ;    

    [picturesMapping addAttributeMappingsFromArray:@[@"image_url"]];

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:picturesMapping
                                                                                   pathPattern:@"get_pictures"
                                                                                       keyPath:nil
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    [self.objectManager addResponseDescriptor:responseDescriptor];
}

- (void) setupItemsMapping
{
itemsMapping = [RKEntityMapping mappingForEntityForName:@"Item" inManagedObjectStore:self.objectManager.managedObjectStore];

itemsMapping.identificationAttributes = @[@"item_id"] ;

[itemsMapping addAttributeMappingsFromArray:@[@"item_id", @"product_id", @"color_id", @"size_id"]];

[itemsMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"images" toKeyPath:@"images" withMapping:picturesMapping]];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:itemsMapping
                                                                                   pathPattern:@"get_items"
                                                                                       keyPath:nil
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[self.objectManager addResponseDescriptor:responseDescriptor];

}

当我尝试加载项目时,我收到一个错误:“由于未捕获的异常 'NSUnknownKeyException' 而终止应用程序,原因:'[<__NSCFConstantString 0x26f72b4> valueForUndefinedKey:]: 这个类不符合 key image_url 的键值编码。'” .

有什么问题?

更新:对不起,第一次发布错误的代码

4

1 回答 1

0

我认为您在这里传递了错误的实体名称-

picturesMapping = [RKEntityMapping mappingForEntityForName:@"Pictures" inManagedObjectStore:self.objectManager.managedObjectStore];

通过“图片”而不是“图片”,例如-

picturesMapping = [RKEntityMapping mappingForEntityForName:@"Picture" inManagedObjectStore:self.objectManager.managedObjectStore];
于 2013-01-10T18:03:40.533 回答