7

我想知道是否有关于如何使用 RESTKit 进行 POST 请求的具体教程。我看过一些教程,但没有找到任何说“这正是你使用 RESTKit 执行 POST 请求的方式”。非常感谢您的帮助。

4

1 回答 1

11

假设您已经有一个映射模型,您可以简单地执行以下操作:

首先,requestDescriptor使用您的 inverseMapping设置 a responseDescriptor,假设您的映射有一个。

//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");
}];

如果您没有映射模型,请告诉我,以便我们尝试其他方法。

于 2013-07-31T00:43:09.393 回答