gpt4 book ai didi

objective-c - RestKit 0.20 - 动态嵌套对象映射

转载 作者:太空狗 更新时间:2023-10-30 03:55:19 26 4
gpt4 key购买 nike

我正在开发 Objective C 项目,该项目使用 RestKit 框架来解析 JSON 响应。现在我需要一些关于以下情况的对象映射设置的帮助:JSON 响应:

    {
"data": {
"SOME.API.Auth": {
"maxVersion": 2,
"minVersion": 1,
"path": "auth.cgi"
},
"SOME.Station": {
"maxVersion": 1,
"minVersion": 1,
"path": "Station/task.cgi"
}
},
"success": true
}

和以下对象:

@interface Response : NSObject
@property (strong, nonatomic) NSArray *data;
@property (assign, nonatomic) BOOL success;
@end


@interface SomeAPIInfo : NSObject
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *path;
@property (strong, nonatomic) NSString *minVersion;
@property (strong, nonatomic) NSString *maxVersion;
@end

这是我的映射设置:

RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[Response class]];
[responseMapping addAttributeMappingsFromDictionary:@{@"success": @"success"}];

RKObjectMapping *dataObjectMapping = [RKObjectMapping mappingForClass:[SomeAPIInfo class]];
dataObjectMapping.forceCollectionMapping = YES;
[dataObjectMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"name"];
[dataObjectMapping addAttributeMappingsFromDictionary:@{
@"(name).path": @"path",
@"(name).minVersion": @"minVersion",
@"(name).maxVersion": @"maxVersion"}];


[responseMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"data"
toKeyPath:@"data"
withMapping:dataObjectMapping]];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
pathPattern:nil
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];


[_objectManager addResponseDescriptor:responseDescriptor];

问题是“数据”对象没有正确映射:NSArray *数据;充满了 2 个“SomeAPIInfo”对象。名称已正确填写,但其他值(路径、最大版本、最小版本)为空。

我做错了什么?还有另一种映射“数据”对象的方法吗?也许直接进入 NSDictionary,因此“Some.API.Auth”将是键,“SomeAPIInfo”将是对象(没有“name”属性)。

感谢您的帮助!

编辑:我认为由于 key 中的点(“SOME.API.Auth”)映射不起作用。

RKMappingOperation.m:
- (NSArray *)simpleAttributeMappings
{
NSMutableArray *mappings = [NSMutableArray array];
for (RKAttributeMapping *mapping in self.nestedAttributeMappings) {
if ([mapping.sourceKeyPath rangeOfString:@"."].location == NSNotFound) {
[mappings addObject:mapping];
}
}

最佳答案

我遇到了由 RestKit 中的错误引起的类似问题 https://github.com/RestKit/RestKit/issues/1532 .我会尝试通过创建具有相同结构但没有点的假 JSON 响应来消除可能的点问题,然后查看问题是否仍然存在。

关于objective-c - RestKit 0.20 - 动态嵌套对象映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14924235/

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