【问题标题】:RestKit 0.20 NSCFConstantString valueForUndefinedKey error while mapping relationshipRestKit 0.20 NSCFConstantString valueForUndefinedKey 映射关系时出错
【发布时间】:2012-12-25 04:16:37
【问题描述】:

所以我正在尝试将 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> valueForUndefinedKey:]:此类不符合键值编码关键 image_url。'"

有什么问题?

更新:抱歉,第一次贴错代码

【问题讨论】:

    标签: ios core-data mapping restkit


    【解决方案1】:

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

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

    传递“图片”而不是“图片”之类-

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

    【讨论】:

    • 不,实体的名字是“图片”(是的,我知道这是个愚蠢的名字)
    • 有问题的是你说“实体'图片'有一个NSString属性'image_url'”。但是在这里你说实体是'图片'。然后检查实体“图片”是否具有属性“image_url”。
    • 实体“图片”必须具有属性“image_url”
    猜你喜欢
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多