ios - RestKit 0.20 NSCFConstantString valueForUndefinedKey映射关系时出错

标签 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”。该实体与称为“图像”的实体“图片”具有一对多关系。实体“图片”具有一个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的编码不兼容。 “

有什么问题?

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

最佳答案

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

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

通过“图片”而不是“图片”,例如-
picturesMapping = [RKEntityMapping mappingForEntityForName:@"Picture" inManagedObjectStore:self.objectManager.managedObjectStore];

关于ios - RestKit 0.20 NSCFConstantString valueForUndefinedKey映射关系时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14263645/

相关文章:

elasticsearch - 在映射中设置自定义类型名称

iOS:MapView 内的 ImageView

core-data - 将 NSManagedObject 转换为子类类型时出现问题

PHP ElasticSearch如何在索引记录之前设置映射?

ios - 在...的对象上找不到属性 NSManagedObjects 上的错误调试属性

ios - 登录和注册 iOS Swift - 核心数据

algorithm - 扑克游戏的商业级随机化

ios - 为什么我无法使用 [transitionContext viewControllerForKey :UITransitionContextFromViewKey] 获取 FromVC 和 ToVC

ios - 检测 MTLTexture 是否为空白

html - 是否可以显示一个日期,例如 22 号,右上角显示小号