所以我试图用这样的对象映射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/