RestKit错误"在keyPath _处将值转换为类型' NSString"的表示失败

时间:2014-07-02 22:24:32

标签: ios restkit foursquare

我是iOS新手,我正在尝试映射Foursquare Explore API并且我一直收到以下错误

E restkit.object_mapping:RKMappingOperation.m:440 Failed transformation of value at keyPath 'name' to representation of type 'NSString': Error Domain=org.restkit.RKValueTransformers.ErrorDomain Code=3002 "Failed transformation of value '(
"Jenkinson's Pavilion",
"The Spot Pizza Grill",
"The Ark Pub & Eatery",
"Jenkinson's Inlet Bar",
"Outside the Box Patio Bar",
"The Off Shore"
)' to NSString: none of the 2 value transformers consulted were successful." UserInfo=0x14Righd3b500 {detailedErrors=(
"Error Domain=org.restkit.RKValueTransformers.ErrorDomain Code=3002 \"The given value is not already an instance of 'NSString'\" UserInfo=0x14d26ab0 {NSLocalizedDescription=The given value is not already an instance of 'NSString'} "Error Domain=org.restkit.RKValueTransformers.ErrorDomain Code=3000 \"Expected an `inputValue` of type `NSNull`, but got a `__NSArrayI`.\" UserInfo=0x14d73060 {NSLocalizedDescription=Expected an `inputValue` of type `NSNull`, but got a `__NSArrayI`.}"

这是我的代码:

- (void)configureRestKit
{
// initialize AFNetworking HTTPClient
NSURL *baseURL = [NSURL URLWithString:@"https://api.foursquare.com"];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];

// initialize RestKit
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];

// setup object mappings
RKObjectMapping *venueMapping = [RKObjectMapping mappingForClass:[Venue class]];
[venueMapping addAttributeMappingsFromDictionary:@{@"name": @"venueName"}];

// register mappings with the provider using a response descriptor
RKResponseDescriptor *responseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:venueMapping
                                             method:RKRequestMethodGET
                                        pathPattern:@"/v2/venues/explore"
                                            keyPath:@"response.groups.items.venue"
                                        statusCodes:[NSIndexSet indexSetWithIndex:200]];

[objectManager addResponseDescriptor:responseDescriptor];
}

现在我只是想映射这个名字',我做错了什么?

1 个答案:

答案 0 :(得分:0)

response.groups.items.venue的关键路径钻得太深,因为groups是一个数组,然后items也是一个数组。钻到第一个数组并在该级别进行映射很好。但是,当您钻取到下一个数组级别时,您将永远尝试将数组转换为单个目标对象。这就是您在日志输出中看到一组名称的原因。

因此,您需要备份和映射到不同的级别,通常使用容器对象,并可能将部分键路径移动到映射而不是描述符。