解决方案:
更新RKPropertyMapping 中的RKStringByReplacingUnderscoresWithBraces 方法,不要用大括号替换括号,然后确保在属性映射中使用大括号。
// RKPropertyMapping
static NSString *RKStringByReplacingUnderscoresWithBraces(NSString *string)
{
return string;
//return [[string stringByReplacingOccurrencesOfString:@"(" withString:@"{"] stringByReplacingOccurrencesOfString:@")" withString:@"}"];
}
// Attribute Mappings
[mapping addAttributeMappingsFromDictionary:@{
@"{name}.param_1":@"param1",
@"{name}.param_2":@"param2"
}];
说明:
https://github.com/RestKit/RestKit/wiki/Object-mapping#handling-dynamic-nesting-attributes
在上述 RestKit 文档中,当您尝试将键映射到属性时,系统会指示您使用 addAttributeMappingFromKeyOfRepresentationToAttribute,然后使用括号映射嵌套键以表示您的属性,后跟“.”。和你的嵌套键。
在 RestKit 中,在从 addAttributeMappingFromKeyOfRepresentationToAttribute 属性映射执行映射后,它会循环遍历所有定义的属性映射,以将占位符替换为键中的实际值,以便可以进行额外的映射操作。在此过程中,它创建一个新的RKPropertyMapping 对象并设置它的sourceKeyPath 和destinationKeyPath。这些属性的属性设置器获取值并用大括号替换括号,然后 RestKit 不会找到带大括号的值的不正确映射。
示例上下文:
// JSON
{ "blake": {
"email": "blake@restkit.org",
"favorite_animal": "Monkey"
}
}
// Class
@interface User : NSObject
@property (nonatomic, copy) NSString* email
@property (nonatomic, copy) NSString* username;
@property (nonatomic, copy) NSString* favoriteAnimal;
@end
// Mapping
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[User class] ];
[mapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"username"];
[mapping addAttributeMappingsFromDictionary:@{
@"(username).email": @"email",
@"(username).favorite_animal": @"favoriteAnimal"
}];
示例映射:
// Resulting Attribute Mappings
@"{username}.email": @"email",
@"{username}.favorite_animal": @"favoriteAnimal"
// 1. The attribute mapping has been performed for 'blake' > 'username'
// 2. RestKit now loops through your attribute mappings to replace '{username}' with 'blake' so your further nested attribute mappings can take place
// 3. Example: @"{username}.email": @"email"
sourceKeyPath = @"{username}.email"
destinationKeyPath = @"email"
* replaces values *
sourceKeyPath = @"blake.email"
destinationKeyPath = @"email"
* creates a new RKPropertyMapping object *
RKPropertyMapping
- sourceKeyPath = @"blake.email"
- destinationKeyPath = @"email"
带括号的示例映射:
// JSON
{ "blake (a.k.a GOAT)": {
"email": "blake@restkit.org",
"favorite_animal": "Monkey"
}
}
// Resulting Attribute Mappings
@"{username}.email": @"email",
@"{username}.favorite_animal": @"favoriteAnimal"
// 1. The attribute mapping has been performed for 'blake (a.k.a GOAT)' > 'username'
// 2. RestKit now loops through your attribute mappings to replace '{username}' with 'blake (a.k.a GOAT)' so your further nested attribute mappings can take place
// 3. Example: @"{username}.email": @"email"
sourceKeyPath = @"{username}.email"
destinationKeyPath = @"email"
* replaces values *
sourceKeyPath = @"blake (a.k.a GOAT).email"
destinationKeyPath = @"email"
* creates a new RKPropertyMapping object *
RKPropertyMapping
- sourceKeyPath = @"blake {a.k.a GOAT}.email" // DOES NOT MATCH
- destinationKeyPath = @"email"