我对使用 restkit 的方式有点困惑,似乎有多种方法可以做同样的事情,在我满足于搞砸它直到它起作用之前,但现在他已经改变了 0.20 中的框架和用法.x,我花了很多时间来转换我的代码,现在花更多的时间试图让它像以前一样工作。而且我已经搜索了一些例子,以及人们声称为他们工作的例子,不要为我做太多,所以在某个地方一定有区别,所以也许有人可以在尝试时告诉我下面的区别至少获取此数据,例如 1 和 2(失败,但这是关于 restkit github 的问题)
response.body={
"player": [
{
"_id": "50585c86ded998e77a000002"
}
]
}
1.
[[RKObjectManager sharedManager].router.routeSet addRoute:[RKRoute routeWithClass:[Player class] pathPattern:@"player/fbid/:fID" method:RKRequestMethodGET]];
为了让你的播放器,像这样:
Player *player = [Player new];
player.playerID = 2;
[[RKObjectManager sharedManager] getObject:player path:nil parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result)
{
// Request
} failure:nil];
2.
像这样使用响应描述符。
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:playerWtfMappingIn pathPattern:nil keyPath:@"player" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];
[tempPlayer setFbID:[result objectForKey:@"id"]];
[objectManager getObject:tempPlayer path:nil parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{
}
failure:nil];
3.
或者实际上是在 RestKit wiki 上拥有自己的示例
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:articleMapping pathPattern:nil keyPath:@"articles" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
NSURL URL = [NSURL URLWithString:@"http://restkit.org/articles"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
RKLogInfo(@"Load collection of Articles: %@", objects);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Operation failed with error: %@", error);
}];
[objectRequestOperation start];
而且我相信还有很多其他方法,我怀疑是否有人可以确认您应该设置路径的方式以及您是否使用一种方法而不是另一种方法,这取决于您尝试映射的数据集及其格式. 然后,其中一些看起来也像是做同样事情的不同方式?谢谢