ios - 使用 RestKit 的嵌套对象映射
<p><p>我在尝试将 JSON 映射到对象结构中时遇到了一些困难:</p>
<pre><code>- Group
+ label
+ type
+ children
- childname1
+ label
+ type
+ value
- childname2
+ label
+ type
+ value
</code></pre>
<p>我确实有一个<code>Group</code>类</p>
<pre><code>@interface CameraGroup : NSObject
@property (nonatomic, copy) NSString *label;
@property (nonatomic, copy) NSString *type;
@property (nonatomic, copy) NSDictionary *children;
@end
</code></pre>
<p>和一个 <code>Child</code> 类</p>
<pre><code>@property (nonatomic, copy) NSString *label;
@property (nonatomic, copy) NSString *type;
@property (nonatomic, copy) NSString *value;
</code></pre>
<p>我的 JSON 如下所示:</p>
<pre><code>{
"actions": {
"label": "Action List",
"type": "section",
"children": {
"action1": {
"label": "This is action1",
"type": "toggle",
"value": 0
},
"action2": {
"label": "This is action2",
"type": "range",
"value": 0
},
"action3": {
"label": "This is action3",
"type": "toggle",
"value": 0
}
}
}
}
</code></pre>
<p>我试图以单个组对象结束,使用 RestKit 持有一个带有键值对操作的 NSDictionary(子对象)(例如 action1 = 标签、类型和值的键和子对象)。</p>
<p>我正在使用以下代码段,但总是以一个单一的空子对象结束:</p>
<pre><code>RKObjectMapping *mappingChild = ];
[mappingSetting addAttributeMappingsFromDictionary:@{
@"label": @"label",
@"type":@"type",
@"value": @"value"
}];
RKObjectMapping *mappingGroup = ];
[mappingGroup addAttributeMappingsFromDictionary:@{
@"label": @"label",
@"type":@"type"
}];
];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor = ;
NSURLRequest *request = ];
RKObjectRequestOperation *operation = [ initWithRequest:request responseDescriptors:@];
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
Group *group = ;
NSLog(@"Mapped the group: %@ of type %@ with children %i", article.label, article.label, );
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Failed with error: %@", );
}];
;
</code></pre>
<p>那么有什么方法可以在不知道“action1”被命名为“action1”的情况下映射 NSDictionary 部分??</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>在玩了之后,我找到了一个似乎可行的解决方案。 <code>responseDescriptor</code>获取<code>Group</code>的映射,它有一个关系映射到一个<code>dynamicMapping</code>,它遍历所有子节点并映射<code>childMapping</code></p>
<pre><code>RKObjectMapping *mappingChild = ];
[mappingSetting addAttributeMappingsFromDictionary:@{
@"label" : @"label",
@"type" : @"type",
@"value" : @"value"
}];
RKDynamicMapping* dynamicMapping = ;
[dynamicMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) {
NSArray *keys = ;
RKObjectMapping *dataMapping = ];
for (NSString *key in keys) {
;
}
return dataMapping;
}];
RKObjectMapping *mappingGroup = ];
[Group addAttributeMappingsFromDictionary:@{
@"label": @"label",
@"type":@"type"
}];
];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mappingGroup
method:RKRequestMethodAny
pathPattern:@"/actions"
keyPath:@"actions"
statusCodes:statusCodes];
</code></pre></p>
<p style="font-size: 20px;">关于ios - 使用 RestKit 的嵌套对象映射,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/21504273/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/21504273/
</a>
</p>
页:
[1]