我正在寻找将我所有的Foursquare列表都存入Core Data。我想使用Restkit来实现这一点。在/ V2 /用户/自我的结构/列出的回应是:在Foursquare的列表API中使用RKDynamicMapping捕获列表
"response": {
"lists": {
"count": 8,
"groups": [
{
"type": "created",
"name": "Lists You've Created",
"count": 6,
"items": [
{
"id": "13250/todos",
"name": "My to-do list", ...
}
{
"id": "13251/something",
"name": "Some List", ...
},
{
"id": "13252/somethingelse",
"name": "Some Other List", ...
}
]
},
{
"type": "followed",
"name": "Lists You've Saved",
"count": 1,
"items": [
{
"id": "5105e3cae4b0e721ca7b400a",
"name": "Portland's Best Coffee - 2012", ...
}
{
...
}
]
}
]
}
正如你可以看到有下的keyPath response.lists.groups 2名列表。最终,我想将这两个列表合并为1,但我很乐意收到2个单独的列表。
我建立了我的映射如下:
RKEntityMapping* listMapping = [RKEntityMapping mappingForEntityForName:[FOFSList entityName]
inManagedObjectStore:objectManager.managedObjectStore];
[listMapping addAttributeMappingsFromDictionary:@{
@"id": @"listID",
@"title": @"name",
@"description": @"desc",
@"user": @"user",
@"following": @"following",
@"collaborative": @"collaborative",
@"canonicalUrl": @"canonicalUrl",
@"venueCount": @"venueCount",
@"visitedCount": @"visitedCount"
}];
RKDynamicMapping *dynamicMapping = [RKDynamicMapping new];
[listMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil
toKeyPath:@"items"
withMapping:dynamicMapping]];
RKResponseDescriptor *listResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:listMapping
method:RKRequestMethodGET
pathPattern:nil
keyPath:@"response.lists.groups"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:listResponseDescriptor];
[dynamicMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) {
if ([[representation valueForKey:@"type"] isEqualToString:@"created"]) {
return listMapping;
} else if ([[representation valueForKey:@"type"] isEqualToString:@"followed"]) {
return listMapping;
}
return nil;
}];
listMapping.identificationAttributes = @[ @"listID" ];
我结束了一个错误:
* Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key propertyMappings.'
我应该使用RKDynamicMappings?是否有一些技巧可以解析这种样式的响应?
您提到:“您需要一个新的非动态映射来处理嵌套的项目。”这就是'listMapping'的作用。它在那里映射动态映射找到的各个对象。我误解你在说什么吗? –