1

现在我要做的是向我的 /api/v1/login 服务发布一个帖子,然后当我得到响应时将其映射到 UserToken 对象中。

现在我有以下代码用于发布 UserLogin 数据,但它没有将响应映射回对象:

- (void) postLogin
{

//Setup Login data
UserLogin *l = [[UserLogin alloc] initWithUsername:[usernameField text] andPassword:[passwordField text]];

//Setup Routes
RKObjectRouter *router = [RKObjectManager sharedManager].router;
[router routeClass:[UserLogin class] toResourcePath:@"/api/v1/login" forMethod:RKRequestMethodPOST];

//Setup Token Mapping
RKObjectMapping *UserTokenMapping = [RKObjectMapping mappingForClass:[UserToken class]];
[UserTokenMapping mapKeyPath:@"authentication_token" toAttribute:@"token"];
//[[[RKObjectManager sharedManager] mappingProvider]setMapping:UserTokenMapping forKeyPath:@""];
[[RKObjectManager sharedManager].mappingProvider registerMapping:UserTokenMapping withRootKeyPath:@""];

//Setup Mapping for UserLogin class
RKObjectMapping *UserLoginMapping = [RKObjectMapping mappingForClass:[UserLogin class]];
[UserLoginMapping mapKeyPath:@"login" toAttribute:@"userName"];
[UserLoginMapping mapKeyPath:@"password" toAttribute:@"passWord"];
[[RKObjectManager sharedManager].mappingProvider registerMapping:UserLoginMapping withRootKeyPath:@""];

[[RKObjectManager sharedManager] postObject:l delegate:self];

}

- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{
  NSLog(@"objects[%d]", [objects count]);
}

响应返回为:

{"message":"Login successful.","authentication_token":"2cksmLGr9hB5kLF3eRTu"}

objectLoader 方法只是返回:

2012-09-26 23:49:57.046 voxpop_iphone[92118:c07] objects[0]

谁能帮我解决我在这里缺少的东西?谢谢大佬!!

4

1 回答 1

0

我遇到了同样的问题并使用不推荐使用的方法来调整正确的映射

反而

[[RKObjectManager sharedManager] postObject:l delegate:self];

我用了

[objectManager postObject:self mapResponseWith:UserTokenMapping delegate:self];
于 2012-12-21T08:52:28.237 回答