【问题标题】:RestKit XML Mapping with same element and attribute name具有相同元素和属性名称的 RestKit XML 映射
【发布时间】:2013-12-07 00:29:42
【问题描述】:

我目前正在使用带有 RKTBXMLSerialization 的 RestKit 来与基于 xml 的 web 服务进行通信。我有一个非常特殊的问题,即对象映射一个呼叫的响应。这个特殊元素有一个与节点本身同名的属性,比如说:

<List ListName="List1">
  <Item Name="ItemName1" Item="ItemValue1" />
  <Item Name="ItemName2" Item="ItemValue2" />
</List>

我对应的 Mapping 是这样的:

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。有没有人已经遇到过这个问题甚至解决方案?

【问题讨论】:

  • 正常的序列化器工作有多奇怪?跟踪记录告诉您什么?

标签: xml xml-serialization restkit rkobjectmapping


【解决方案1】:

正如我已经发布的那样,我现在也尝试了 RKXMLReaderSerialization,问题就消失了。代码方面的解决方案正在改变

[RKMIMETypeSerialization registerClass:[RKTBXMLSerialization class] forMIMEType:@"application/xml"];

[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"application/xml"];

我会在 GitHub 上发布关于 RKTBXMLSerialization 的问题,所以它可能会在未来的版本中得到修复。

【讨论】:

    猜你喜欢
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    相关资源
    最近更新 更多