gpt4 book ai didi

objective-c - 使用对象之间的公共(public)键值对将 NSArray 拆分为较小的键值编码数组

转载 作者:太空狗 更新时间:2023-10-30 03:49:34 24 4
gpt4 key购买 nike


我正在尝试找到一个很好的解决方案,将一组字典拆分成一个较小的字典,这些字典由它们之间的共同值作为键控。
这是一个 JSON 示例,我从这里开始:

{
"field": [
{
"id": 6,
"name": "Andrea"
},
{
"id": 67,
"name": "Francesco"
},
{
"id": 8,
"name": "Maria"
},
{
"id": 6,
"name": "Paolo"
},
{
"id": 67,
"name": "Sara"
}
]
}

我想得到这样的结果:

{
"field": [
{
"6": [
{
"name": "Andrea",
"id": 6
},
{
"name": "Paolo",
"id": 6
}
],
"67": [
{
"name": "Sara",
"id": 67
},
{
"name": "Francesco",
"id": 67
}
],
"8": [
{
"name": "Maria",
"id": 8
}
]
}
]
}

我设法使用这段代码,它有效,但我想知道是否存在更正确和更快的东西:

    NSArray * array = ...;
NSSortDescriptor *sorter1=[[NSSortDescriptor alloc]initWithKey:@"id" ascending:YES selector:@selector(compare:)];
NSSortDescriptor *sorter2=[[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];
NSArray *sortDescriptors=[NSArray arrayWithObjects:sorter1,sorter2,nil];
array = [array sortedArrayUsingDescriptors:sortDescriptors];
//////////////////////////////SPLITTER
NSMutableArray * subcategorySplittedArray = [[NSMutableArray alloc]initWithCapacity:30];
NSNumber * lastID=[[array objectAtIndex:0]objectForKey:@"id"];
NSMutableArray * shopArray = [[NSMutableArray alloc]initWithCapacity:100];
NSMutableDictionary * catDict = nil;
for (NSDictionary * dict in array) {
NSNumber * catID = [dict objectForKey:@"id"];
if ([lastID isEqualToNumber:catID]) {
[shopArray addObject:dict];
}
else {

catDict = [[NSMutableDictionary alloc]init ];
[catDict setObject:[shopArray copy] forKey:lastID];
[subcategorySplittedArray addObject:catDict];
[shopArray removeAllObjects];
[shopArray addObject:dict];
lastID = catID;
}
}
catDict = [[NSMutableDictionary alloc]init ];
[catDict setObject:[shopArray copy] forKey:lastID];
[subcategorySplittedArray addObject:catDict];
////////////////////////////////////
return subcategorySplittedArray;

}

最佳答案

NSMutableDictionary* result = [NSMutableDictionary dictionary];
NSArray* ids = [array valueWithKey:@"id"];
NSSet* uniqueIDs = [NSSet setWithArray:ids];
for (NSNumber* anID in uniqueIDs)
{
NSPredicate* pred = [NSPredicate predicateWithFormat:@"id == %@", anID];
NSArray* dictsForID = [array filteredArrayUsingPredicate:pred];
[result setObject:dictsForID forKey:anID];
}

如果有很多 ID,您可以通过在循环外构建一个带有变量引用的谓词来加快速度,然后只需将变量替换为每次循环生成特定于 id 的谓词循环。

顺便说一句,在你的问题中,出于某种原因,结果“字段”仍然是一个数组。我认为没有必要。


更新为仅通过一次:

NSMutableDictionary* result = [NSMutableDictionary dictionary];
for (NSDictionary* dict in array)
{
NSNumber* anID = [dict objectForKey:@"id"];
NSMutableArray* resultsForID = [result objectForKey:anID];
if (!resultsForID)
{
resultsForID = [NSMutableArray array];
[result setObject:resultsForID forKey:anID];
}

[resultsForID addObject:dict];
}

关于objective-c - 使用对象之间的公共(public)键值对将 NSArray 拆分为较小的键值编码数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10118304/

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