RESTKit:带有字典的 RKMappingResult

Posted

技术标签:

【中文标题】RESTKit:带有字典的 RKMappingResult【英文标题】:RESTKit: RKMappingResult with Dictionary 【发布时间】: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:

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

谢谢。 [[RKObjectManager sharedManager] cancelAllObjectRequestOperationsWithMethod:RKRequestMethodAny matchingPathPattern:nil];我可以用它来停止所有请求吗? 我认为您不能使用零路径模式。您可以从队列中获取所有操作并取消它们。

以上是关于RESTKit:带有字典的 RKMappingResult的主要内容,如果未能解决你的问题,请参考以下文章

如何使用带有 RKObjectMapping 的 RestKit 0.24 将本地 JSON 字符串映射到对象?

映射后向 RestKit 映射结果字典添加值

如何使用 RestKit 将日期组件字典映射到 NSDate

restkit 映射嵌套数组为空(带有 json 响应的嵌套 restkit 请求)

带有本地 XML 的 RestKit

带有 NSDateFormatter 的 RestKit