在过去,我为每个 RKResponseDescriptor 以及每个 RKObjectManager get/post/patch/delete 方法指定了路径参数。这行得通,但 RKRouter 似乎是更好、更模块化的方法。
我在设置包含嵌套变量的动态路由时遇到问题。例如:
添加路线
RKRoute *locationsRoute = [RKRoute routeWithClass:[Location class] pathPattern:@"users/:userID/locations/:locationID" method:RKRequestMethodAny];
[[RKObjectManager sharedManager].router.routeSet addRoutes:@[locationsRoute]];
设置响应描述符
RKResponseDescriptor *locationResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:locationResponseMapping
method:RKRequestMethodAny
pathPattern:nil
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
调用对象管理器
[[RKObjectManager sharedManager] getObject:[Location class] path:nil parameters:nil success:nil failure:nil];
在使用路由器之前,我会在对象管理器调用路径中包含用户 ID。 (如:path:[NSString stringWithFormat:@"users/%@/locations/%@", [self getCurrentUser].userID, location.locationID]
)。但是,在使用路由器时,我似乎无法弄清楚如何指定它。 执行此操作的正确方法是什么?我希望用户 ID 不必硬编码到路由路径中。
重要的是要注意我所有的映射都设置正确,在尝试实现路由之前一切都完美无缺。感谢您的帮助!
最佳答案
你的路线没问题,这是错误的:
[[RKObjectManager sharedManager] getObject:[Location class] ...
因为您需要传递 Location
类的实例(不是类对象)并且该实例需要具有 userID
和 locationID
设置属性以便将它们注入(inject)路由路径模式。
关于ios - RestKit Router - 如何使用嵌套变量实现路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21117295/