如何在Restkit 0.20.3中获取基于对象的响应

时间:2014-12-23 05:28:56

标签: ios restkit

您在我之前的几个版本0.10.0的项目中使用过restkit。但是现在我将使用新的restkit v0.20.3。

我遵循了从HERE升级10.0到20.0的所有步骤。

我能够执行我的请求并且响应也成功归功于博客。但我可以获得我的响应对象的属性。这是非常令人震惊的。我只能通过[data valueForKey:@""]来获得价值,这在我的restkit中是不行的。

任何人都可以告诉我如何从对象的属性中获取价值。

[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        NSLog(@"%lu", (unsigned long)mappingResult.array.count);
        DataForResponse *data = [mappingResult.array objectAtIndex:0];
        User *user = [data valueForKey:@"user"];

        User *user = [[data.user allObjects] firstObject];
        NSLog(@"%@",[user valueForKey:@"email"]);
        RKLogInfo(@"Load collection of Users: %@", mappingResult.array);
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        NSLog(@"%@",operation.HTTPRequestOperation.responseString);
        RKLogError(@"Operation failed with error: %@", error);
    }];

我的目标是从user.email

获取电子邮件的价值

1 个答案:

答案 0 :(得分:0)

搜索后&基于我的restkit知识,下面的代码已经完成。

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[DataForResponse objectMappingForDataResponse:LOGIN] method:RKRequestMethodPOST pathPattern:nil keyPath:@"data" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    [rkomForLogin addResponseDescriptor:responseDescriptor];
    RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
    operation.targetObject =  data;

    [rkomForLogin postObject:nil path:@"login" parameters:dict success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        // Handled with articleDescriptor
        NSLog(@"%@",operation.HTTPRequestOperation.responseString);
        DataForResponse *data  = [mappingResult.array objectAtIndex:0];
        User *user  = [[data.user allObjects] firstObject];
        NSLog(@"%@",[user email]);
        NSLog(@"%ld",operation.HTTPRequestOperation.response.statusCode);
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        // Transport error or server error handled by errorDescriptor
        NSLog(@"%@",operation.HTTPRequestOperation.responseString);
        RKLogError(@"Operation failed with error: %@", error);
    }];

这是我在restkit 0.10中获得的基于对象的响应,因此这是执行请求的方法。