RestKit 0.20 NSCFConstantString valueForUndefinedKey映射关系时出错

时间:2013-01-10 17:23:05

标签: ios core-data mapping restkit

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

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

我将它直接映射到Core Data实体'Item'。该实体与实体'Pictures'称为'images'之间存在一对多的关系。实体“图片”具有一个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:]:此类不符合键值编码关键字image_url。'“

有什么问题?

更新:抱歉,第一次发布了错误的代码

1 个答案:

答案 0 :(得分:0)

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

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

传递“图片”而非“图片”,如 -

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