我目前正在使用带有 RKTBXMLSerialization 的 RestKit 来与基于 xml 的 web 服务进行通信。我有一个非常特殊的问题,即对象映射一个呼叫的响应。这个特殊元素有一个与节点本身同名的属性,比如说:
<List ListName="List1">
<Item Name="ItemName1" Item="ItemValue1" />
<Item Name="ItemName2" Item="ItemValue2" />
</List>
我相应的映射如下所示:
RKObjectMapping *listMapping = [RKObjectMapping mappingForClass:[ListModel class]];
[listMapping addAttributeMappingsFromArray:@[@"ListName"]];
RKObjectMapping *itemMapping = [RKObjectMapping mappingForClass:[ItemModel class]];
[itemMapping addAttributeMappingsFromArray:@[@"Name", @"Item"]];
RKRelationshipMapping *itemRelationship = [RKRelationshipMapping relationshipMappingFromKeyPath:@"Item" toKeyPath:@"Items" withMapping:itemMapping];
[listMapping addPropertyMapping:itemRelationship];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:listMapping method:RKRequestMethodGET pathPattern:nil keyPath:@"List" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[[RKObjectManager sharedManager] addResponseDescriptorsFromArray:@[responseDescriptor]];
使用这些模型类:
@interface ListModel : NSObject
@property (nonatomic, strong) NSArray* Items;
@property (nonatomic, copy) NSString* ListName;
@end
@interface ItemModel : NSObject
@property (nonatomic, copy) NSString* Name;
@property (nonatomic, copy) NSString* Item;
@end
我得到的错误信息是:
'NSUnknownKeyException', reason: '[<__NSCFString 0xb816420> valueForUndefinedKey:]: this class is not key value coding-compliant for the key Item.'
我已经尝试了很多,并且可以将问题追溯到 xml 结构中的重复名称“项目”。由于我无法影响响应模式,我需要一个客户端解决方案。
问题也可能在 RKTBXMLSerialization (TBXML) 内部,所以我将尝试 RKXMLReaderSerialization。有没有人已经遇到过这个问题甚至解决方案?