gpt4 book ai didi

ios - 在Restkit中设置映射的rootkeypath

转载 作者:行者123 更新时间:2023-11-29 03:07:35 25 4
gpt4 key购买 nike

我有以下 JSON:

{
"IdList": [
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10"
],
“Persons”: [
{
"Id": 2,
"Name": “James”,
"Description": “Tall”,
"Items": [

{
"Id": 1051,
"Name": “Hat”,
"Description": "",
“Accesories”: []
}]
]}

我是这样使用 RKResponseDescriptor 的:

 RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[MappingProvider validationMapping] method:RKRequestMethodGET pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[self addResponseDescriptor:responseDescriptor];

我的验证映射包含以下内容:

    + (RKDynamicMapping *)validationMapping {

RKDynamicMapping * dynamicMapping = [RKDynamicMapping new];

[dynamicMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) {

NSArray *personsAray = [representation objectForKey:@"Persons"];

if (!personArray || !personArray.count) {
//Fail operation no contents
return nil;


}else {

NSArray *idsArray = [representation objectForKey:@"IdList"];

if (!idsArray || !idsArray.count) {
//Fail operation no contents
return nil;

}else {

if (idsArray) {

[[NSUserDefaults standardUserDefaults] setObject:idsArray forKey:kIds];
}

[[NSUserDefaults standardUserDefaults] synchronize];

}

NSArray *categoryArray = [menuArray valueForKey:@"Items"];

if (!categoryArray || !categoryArray.count) {

return nil;
}else {

// If we have some data map the response!
return [self theMapping];

}

}

return nil;

}];

return dynamicMapping;



}

+ (RKEntityMapping *)theMapping {
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Person" inManagedObjectStore:[[CoreDataManager sharedInstance] objectStore]];

// I want to start the mapping from Person, how do I do that?
[mapping mappingForSourceKeyPath:@"Persons"];


mapping.assignsNilForMissingRelationships = YES;
mapping.assignsDefaultValueForMissingAttributes = YES;

[mapping addAttributeMappingsFromDictionary:@{
@"Id": @"remoteID",
@"Name": @"name",
}];


mapping.identificationAttributes = @[ @"remoteID" ];

[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Items"
toKeyPath:@"misc"
withMapping:[MappingProvider productMapping]]];




return mapping;
}

如何告诉 restKit 从我的“theMapping”方法中的 Persons 键路径开始映射?我试过使用 [mapping mappingForSourceKeyPath:@"Persons"]; 但没有成功。

最佳答案

在这种情况下,您通常会使用 2 个响应描述符。

您的第一个响应描述符将使用键路径为 IdList 的动态映射。这有点滥用,并将表示存储为用户默认值并返回 nil。

您的第二个响应描述符将使用带有 Items 键路径的法线映射(从外观上看,尽管您的代码使用 undefined variable ),并且依赖 RestKit 不执行任何映射(如果存在)没有结果(尽管在有要删除的获取请求 block 的情况下,您需要使用另一个动态映射)。

mappingForSourceKeyPath: 返回具有指定键路径的接收器上的属性映射,因此与您想要执行的操作完全无关。它是响应描述符上深入数据结构的关键路径。您可以使用映射中的关键路径进行钻取,但这会钻取到对象,而不是字典。

关于ios - 在Restkit中设置映射的rootkeypath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22590917/

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