使用与关系名称不同的密钥路径添加Restkit关系映射

时间:2014-04-15 17:44:20

标签: ios objective-c restkit

我正在尝试将关系映射添加到我的一个请求映射中。

POST请求应该发送正文内容:

  

{ “帐户”:{ “键”: “the_key_goes_here”, “TERMS_OF_SERVICE”:1, “person_attributes”:{ “电子邮件”: “test@example.com”}}}

我已将帐户映射设置为:

NSMutableDictionary *accountAttributes = [NSMutableDictionary dictionaryWithDictionary: @{@"key":@"key",@"terms_of_service":@"terms_of_service",@"source":@"source"}];
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:accountAttributes];

现在,为了尝试将person_attributes添加到请求中,我执行以下操作:

NSMutableDictionary *userAttributes = [NSMutableDictionary dictionaryWithDictionary: @{@"first_name":@"first_name",@"last_name":@"last_name",@"email":@"email"}];
RKObjectMapping *userMapping = [RKObjectMapping requestMapping];
[userMapping addAttributeMappingsFromDictionary:userAttributes];
[requestMapping addRelationshipMappingWithSourceKeyPath:@"person" mapping:userMapping];

但是,它始终发送为:

  

{ “帐户”:{ “键”: “the_key_goes_here”, “TERMS_OF_SERVICE”:1, “人”:{ “电子邮件”: “test@example.com”}}}

尝试将 person 更改为 person_attributes 时,我将relationshipmappingwithsourcekeypath修改为:

[requestMapping addRelationshipMappingWithSourceKeyPath:@"person_attributes" mapping:userMapping];

但是,它每次都会失败,并说明:the entity Account is not key value coding-compliant for the key "person_attributes"

我理解这是因为Account没有person_attributes值。有没有办法将密钥路径添加为person,但是将其作为person_attributes发送?

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:4)

您需要使用RKRelationshipMapping类和relationshipMappingFromKeyPath:toKeyPath:withMapping:方法创建关系,然后使用addPropertyMapping:进行设置。