gpt4 book ai didi

ios - 使用 RestKit 的嵌套对象映射

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:44:03 24 4
gpt4 key购买 nike

我正在尝试将以下 JSON 映射到对象结构中:

- Group
+ label
+ type
+ children
- childname1
+ label
+ type
+ value

- childname2
+ label
+ type
+ value

我有一个Group

@interface CameraGroup : NSObject
@property (nonatomic, copy) NSString *label;
@property (nonatomic, copy) NSString *type;
@property (nonatomic, copy) NSDictionary *children;
@end

和一个

@property (nonatomic, copy) NSString *label;
@property (nonatomic, copy) NSString *type;
@property (nonatomic, copy) NSString *value;

我的 JSON 如下所示:

{
"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
}
}
}
}

我试图以单个对象组结束,使用 RestKit 保存一个 NSDictionary(子对象)和键值对操作(例如 action1 = 标签、类型和值的键和子对象)。

我正在使用以下代码片段,但总是以单个空子对象结尾:

RKObjectMapping *mappingChild = [RKObjectMapping mappingForClass:[Child class]];
[mappingSetting addAttributeMappingsFromDictionary:@{
@"label": @"label",
@"type": @"type",
@"value": @"value"
}];

RKObjectMapping *mappingGroup = [RKObjectMapping mappingForClass:[Group class]];
[mappingGroup addAttributeMappingsFromDictionary:@{
@"label": @"label",
@"type": @"type"
}];
[mappingGroup addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"children" toKeyPath:@"children" withMapping:mappingSetting]];


NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mappingGroup method:RKRequestMethodAny pathPattern:@"/action" keyPath:@"actions" statusCodes:statusCodes];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.178.1:1337/actions"]];
RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
Group *group = [result firstObject];
NSLog(@"Mapped the group: %@ of type %@ with children %i", article.label, article.label, [group.children count]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Failed with error: %@", [error localizedDescription]);
}];
[operation start];

那么有没有办法在不知道“action1”被命名为“action1”的情况下映射 NSDictionary 部分??

最佳答案

在尝试之后,我找到了一个似乎可行的解决方案。 responseDescriptor 获取具有关系映射的 GroupdynamicMapping 的映射,它遍历所有子节点并映射 childMapping

RKObjectMapping *mappingChild = [RKObjectMapping mappingForClass:[Child class]];

[mappingSetting addAttributeMappingsFromDictionary:@{
@"label" : @"label",
@"type" : @"type",
@"value" : @"value"
}];

RKDynamicMapping* dynamicMapping = [RKDynamicMapping new];
[dynamicMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) {

NSArray *keys = [representation allKeys];

RKObjectMapping *dataMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];

for (NSString *key in keys) {
[dataMapping addRelationshipMappingWithSourceKeyPath:key mapping:mappingChild];
}

return dataMapping;

}];

RKObjectMapping *mappingGroup = [RKObjectMapping mappingForClass:[Group class]];
[Group addAttributeMappingsFromDictionary:@{
@"label": @"label",
@"type": @"type"
}];

[Group addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"children" toKeyPath:@"children" withMapping:dynamicMapping]];







NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mappingGroup
method:RKRequestMethodAny
pathPattern:@"/actions"
keyPath:@"actions"
statusCodes:statusCodes];

关于ios - 使用 RestKit 的嵌套对象映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21504273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com