1

这个问题有两个部分,都是关于 RestKit 的:

  1. 我们如何发布 2 个值的电子邮件和密码,并使用对象映射器处理响应
  2. 我们如何在响应中映射 2 个对象

我们期待以下 JSON 响应:

{ 
    "code" : 0,
    "error_string" : "OK.",
    "message" : "OK.",
    "token" : { 
        "app_id" : "1",
        "created" : "2011-08-19 11:30:31",
        "token" : "ecb8862189974248233dfcc7e8fc1e4514e16972",
        "user_id" : "1"
    },
    "user" : { 
        "avatar_url" : "",
        "created" : "2011-08-19 11:29:21",
        "email" : "x@x.com",
        "forename" : "Matthew",
        "gender" : "M",
    }
}

映射它的正确方法是什么,我们为用户和令牌设置了一个类,但是我看到的所有示例似乎都没有显示像这样的东西,其中响应有两个部分,这是我们的代码在这一刻:

// Mapping for User
RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[User class]];
[userMapping mapKeyPath:@"created" toAttribute:@"created"];
[userMapping mapKeyPath:@"avatar_url" toAttribute:@"avatarURL"];
[userMapping mapKeyPath:@"gender" toAttribute:@"gender"];
[userMapping mapKeyPath:@"email" toAttribute:@"email"];
[[RKObjectManager sharedManager].mappingProvider setMapping:userMapping forKeyPath:@"user"];

// Mapping for Token
RKObjectMapping* tokenMapping = [RKObjectMapping mappingForClass:[Token class]];
[tokenMapping mapAttributes:@"user_id", @"app_id", @"token", @"created", nil];
[[RKObjectManager sharedManager].mappingProvider setMapping:tokenMapping forKeyPath:@"token"];

// Load the object model via RestKit
[[[RKObjectManager sharedManager] client] setValue:@"xxxxxxxxxxxxxxxxxxx" forHTTPHeaderField:@"X-API-KEY"];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/users/authenticate/"  delegate:self];

感谢您对此提供的任何帮助,到目前为止,我很喜欢 RestKit!

4

1 回答 1

0

您应该为此映射配置所需的一切。您可能想要使用 didLoadObjectDictionary: 委托回调,以便您可以通过可映射的 keyPath 识别对象。否则,如果您使用 didLoadObjects:,您应该只得到一个包含 User & Token 对象的数组。

于 2011-08-26T14:32:01.323 回答