【问题标题】:RESTKit: RKMappingResult with DictionaryRESTKit:带有字典的 RKMappingResult
【发布时间】:2014-03-22 19:51:11
【问题描述】:

下图中是我返回的 JSON。

下面是我的代码映射:

+(RKMapping *)googleMapping
{
    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[mappingModel class]];

    [mapping addAttributeMappingsFromDictionary:@{
                                                  @"description" : @"description",
                                                  @"reference" : @"reference"
                                                  }];
    return mapping;
}

获取:

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping
                                                                                            method:RKRequestMethodGET
                                                                                       pathPattern:nil
                                                                                       keyPath:@"predictions" statusCodes:statusCodeSet];
RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request
                                                                    responseDescriptors:@[responseDescriptor]];

[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

    NSLog(@"mappingResults %@", mappingResult);

} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"mappingResults Error %@", error);

}];
[operation start];

NSLog:

2014-03-22 12:42:03.988 Google+RESTKit[21229:60b] I restkit.network:RKObjectRequestOperation.m:180 GET 'https://maps.googleapis.com/maps/api/place/autocomplete/json?input=cofee&sensor=true&key=1234&location=0.000000,0.000000&radius=100.000000'
2014-03-22 12:42:04.031 Google+RESTKit[21229:6a03] I restkit.network:RKObjectRequestOperation.m:250 GET 'https://maps.googleapis.com/maps/api/place/autocomplete/json?input=cofee&sensor=true&key=1234&location=0.000000,0.000000&radius=100.000000' (200 OK / 5 objects) [request=0.0430s mapping=0.0007s total=0.0474s]
2014-03-22 12:42:04.032 Google+RESTKit[21229:60b] mappingResults <RKMappingResult: 0x1733ecf0, results={
    predictions =     (
    "Coffee Road, Modesto, CA, United States",
    "Coffee Bay, Eastern Cape, South Africa",
    "Coffee Road, Bakersfield, CA, United States",
    "Coffeeville, MS, United States",
    "Coffeen, IL, United States"
    );
}>

返回的mappingResult应该是一个带有NSDictionary Objects的数组吧?它似乎在数组中返回“描述”的值,并且没有“参考”值。我究竟做错了什么?

返回的 JSON:

【问题讨论】:

    标签: objective-c json restkit restkit-0.20


    【解决方案1】:

    description 是已定义的方法名称,因此通过添加您自己的同名属性,您将覆盖默认实现。所以,你确实有你期望的对象,但是日志打印了你不期望的东西。

    更改您的资源名称,使其不与description 重叠。

    【讨论】:

    • 谢谢。 [[RKObjectManager sharedManager] cancelAllObjectRequestOperationsWithMethod:RKRequestMethodAny matchingPathPattern:nil];我可以用它来停止所有请求吗?
    • 我认为您不能使用零路径模式。您可以从队列中获取所有操作并取消它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多