我想知道是否有关于如何使用RESTKit执行POST请求的特定教程.我看过一些教程,但我没有发现任何说法,“这正是你用RESTKit做POST命令的方式.”非常感谢帮助.
解决方法
假设您已经有一个映射模型,您可以简单地执行此操作:
首先,使用responseDescriptor的inverseMapping设置requestDescriptor,假设您有一个映射.
//This is used for mapping responses,you already should have one of this. PS:[Data mapping] is a method that returns an RKObjectMapping for my model. You should create yours or use a prevIoUs created one RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[Data mapping] pathPattern:nil keyPath:@"data" statusCodes:statusCodeSet]; [[RKObjectManager sharedInstance] addResponseDescriptor:responseDescriptor]; //Inverse mapping,to perform a POST RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[[Data mapping] inverseMapping] objectClass:[Data class] rootKeyPath:nil]; [[RKObjectManager sharedInstance] addRequestDescriptor:requestDescriptor];
之后,要执行POST,只需调用下面的方法即可. Restkit将获取您尝试发布的实例,将其序列化并发送到所选路径.
[[RKObjectManager sharedInstance] postObject:instanceOfYourModel path:yourPathHere parameters:nil success:^(RKObjectRequestOperation *operation,RKMappingResult *mappingResult) { NSLog(@"Success"); } failure:^(RKObjectRequestOperation *operation,NSError *error) { NSLog(@"Error"); }];
如果您没有映射模型,请告诉我们,以便我们可以尝试其他方法.