(
{
data = (
"<Place: 0x7ff9ba4af4f0>"
);
type = Aquarium;
},
{
data = (
"<Place: 0x7ff9ba467d70>",
"<Place: 0x7ff9ba4bdc10>",
"<Place: 0x7ff9ba4c1a00>",
"<Place: 0x7ff9ba4d4f60>",
"<Place: 0x7ff9ba4e5090>"
);
type = "Amusement Park";
},
{
data = (
"<Place: 0x7ff9ba444f60>",
"<Place: 0x7ff9ba4dced0>",
"<Place: 0x7ff9ba4e9000>",
"<Place: 0x7ff9ba4f4ab0>"
);
type = Spa;
},
{
data = (
"<Place: 0x7ff9ba465380>",
"<Place: 0x7ff9ba4b9d10>",
"<Place: 0x7ff9ba4d1140>",
"<Place: 0x7ff9ba4d8f60>"
);
type = "Art Gallery";
},
{
data = (
"<Place: 0x7ff9ba763100>",
"<Place: 0x7ff9ba76f520>"
);
type = "Art Gallery";
},
{
data = (
"<Place: 0x7ff9ba763160>",
"<Place: 0x7ff9ba68ffe0>",
"<Place: 0x7ff9ba6a56c0>"
);
type = "Amusement Park";
},
{
data = (
"<Place: 0x7ff9ba7630a0>",
"<Place: 0x7ff9ba7676f0>",
"<Place: 0x7ff9ba6ae020>",
"<Place: 0x7ff9ba69ba30>"
);
type = Bakery;
},
{
data = (
"<Place: 0x7ff9ba678520>",
"<Place: 0x7ff9ba76e550>"
);
type = Spa;
}
)
这是我的代码。我试图将两个字典与相同的密钥组合。下面的代码和我跟随stackoverflow网站NSArray of NSDictionaries - merge dictionaries with same key value pair
-(NSMutableArray*)combinedArray:(NSMutableArray*)array
{
NSMutableArray* combined = [[NSMutableArray alloc]init];
for(id key in [NSSet setWithArray:[array valueForKeyPath:@"type"]])
{
if([key isKindOfClass:[NSNull class]])
continue;
// Sub array with only id = key
NSArray* filtered = [array filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSDictionary* evaluatedObject, NSDictionary *bindings)
{
return [evaluatedObject valueForKey:@"data"] && [[evaluatedObject valueForKey:@"type"] isEqual:key];
}]];
// Grab the data
NSArray *arrtyp = [filtered valueForKeyPath:@"data"];
// add the new dictionary
[combined addObject:@{ @"type":key, @"data": arrtyp }];
}
NSLog(@"%@",combined);
return combined;
}
我得到的输出如下:
{
data = (
(
"<Place: 0x7fa4b164a3f0>",
"<Place: 0x7fa4b1651940>",
"<Place: 0x7fa4b14c7890>",
"<Place: 0x7fa4b149e6c0>",
"<Place: 0x7fa4b1664e70>",
"<Place: 0x7fa4b1678530>",
"<Place: 0x7fa4b1668c90>",
"<Place: 0x7fa4b16708b0>"
),
(
"<Place: 0x7fa4b16e2b80>",
"<Place: 0x7fa4b2808470>"
),
(
"<Place: 0x7fa4b17515b0>",
"<Place: 0x7fa4b1772b70>"
)
);
type = "Art Gallery";
},
{
data = (
(
"<Place: 0x7fa4b164a390>",
"<Place: 0x7fa4b1674630>",
"<Place: 0x7fa4b1680670>",
"<Place: 0x7fa4b168bd20>"
),
(
"<Place: 0x7fa4b16ea8e0>",
"<Place: 0x7fa4b280cc60>",
"<Place: 0x7fa4b28191d0>"
),
(
"<Place: 0x7fa4b174c8e0>",
"<Place: 0x7fa4b1751550>",
"<Place: 0x7fa4b1765e90>"
)
);
type = Spa;
},
我想要的是:
{
data = (
"<Place: 0x7fa4b164a3f0>",
"<Place: 0x7fa4b1651940>",
"<Place: 0x7fa4b14c7890>",
"<Place: 0x7fa4b149e6c0>",
"<Place: 0x7fa4b1664e70>",
"<Place: 0x7fa4b1678530>",
"<Place: 0x7fa4b1668c90>",
"<Place: 0x7fa4b16708b0>"
"<Place: 0x7fa4b16e2b80>",
"<Place: 0x7fa4b2808470>"
"<Place: 0x7fa4b17515b0>",
"<Place: 0x7fa4b1772b70>"
)
);
type = "Art Gallery";
},
{
data = (
"<Place: 0x7fa4b164a390>",
"<Place: 0x7fa4b1674630>",
"<Place: 0x7fa4b1680670>",
"<Place: 0x7fa4b168bd20>"
"<Place: 0x7fa4b16ea8e0>",
"<Place: 0x7fa4b280cc60>",
"<Place: 0x7fa4b28191d0>"
"<Place: 0x7fa4b174c8e0>",
"<Place: 0x7fa4b1751550>",
"<Place: 0x7fa4b1765e90>"
)
);
type = Spa;
},
那么如何将两个字典与相同的键组合起来呢?我怎样才能将这两个数组合二为一。我将拥有那些&#34;类型&#34;作为tableview中的部分和&#34;数据&#34;在行中。我被困住了。需要帮助 谢谢。
答案 0 :(得分:1)
放在下面的功能:
- (NSMutableArray *)combinedArray:(NSMutableArray *)array
{
NSMutableArray *combined = [[NSMutableArray alloc]init];
for (id key in [NSSet setWithArray:[array valueForKeyPath:@"type"]])
{
if ([key isKindOfClass:[NSNull class]])
continue;
NSArray *filtered = [array filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSDictionary* evaluatedObject, NSDictionary *bindings) {
return [evaluatedObject valueForKey:@"data"] && [[evaluatedObject valueForKey:@"type"] isEqual:key];
}]];
NSMutableArray *marrtyp = [[NSMutableArray alloc] init];
for (int i = 0; i < filtered.count; i++) {
[marrtyp addObjectsFromArray:[[filtered objectAtIndex:i] valueForKey:@"data"]];
}
[combined addObject:@{@"type": key, @"data": marrtyp}];
}
NSLog(@"%@",combined);
return combined;
}
这很好用。 。 。
答案 1 :(得分:1)
当使用for循环时,我们也需要处理分配的内存。如果不是没有问题,它将照顾ARC。但是,如下面的代码清除已分配的数组是最佳做法。
- (NSMutableArray *)combinedArray:(NSMutableArray *)array
{
NSMutableArray *combined = [[NSMutableArray alloc]init];
for (id key in [NSSet setWithArray:[array valueForKeyPath:@"type"]])
{
if ([key isKindOfClass:[NSNull class]])
continue;
NSArray *filtered = [array filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSDictionary* evaluatedObject, NSDictionary *bindings) {
return [evaluatedObject valueForKey:@"data"] && [[evaluatedObject valueForKey:@"type"] isEqual:key];
}]];
NSMutableArray *arr = [[NSMutableArray alloc] init];
for (int i = 0; i < filtered.count; i++) {
[arr addObjectsFromArray:[[filtered objectAtIndex:i] valueForKey:@"data"]];
}
[combined addObject:@{@"type": key, @"data": arr}];
arr = nil;
filtered = nil;
}
NSLog(@"%@",combined);
return combined;
}