Map url parameters to objects using RESTKit

南笙酒味 提交于 2019-12-28 15:36:20

问题


is there a way to map the parameters in an URL to the results?

I got a rest service where the user can search something by its ID. The path is search/:id The results contains the name and other properties but not the ID.

I could do something like this:

NSArray *array = mappingResult.array;
for (Item *item in array) {
        [item setId:itemID];
}

but I hope there is a nicer way...

Thanks for any hints
Xean


回答1:


You want to use the path pattern you specify in your response descriptor. Then you want to use routing (RKRoute) and metadata during your mapping. The metadata includes a routing section which gives access to the parameters extracted from the URL path.

Some info on metadata here (the docs are a little lacking).

In your mapping you want to use:

@metadata.routing.parameters.id

As the mapping source key path.


To make routing work you need to add the route to your object manager:

[manager.router.routeSet addRoute:...

And then you need to make the request in a way that means the route is used, like getObjectsAtPathForRouteNamed:object:parameters:success:failure:.




回答2:


You probably need to specify the pathPattern and keyPath of your response descriptor::

RKResponseDescriptor *responseDescriptor =
  [RKResponseDescriptor
   responseDescriptorWithMapping:itemMapping
   method:RKRequestMethodAny
   pathPattern:@"search/:id"
   keyPath:@"item"
   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];


来源:https://stackoverflow.com/questions/18324033/map-url-parameters-to-objects-using-restkit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!