(
{
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;
}
)
This is my code.I tried to combine two dictionary with same key.The code below and i followed the stackoverflow site 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;
}
The output I got is below:
{
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;
},
How I want is:
{
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;
},
So how can I combine two dictionary with same key else how can i get those two array to one.I will have those "type" as section in tableview and "data" in rows.I am stuck.Need help Thank you.
When using for loop we need to take care of allocated memory too. If not there is no problem, it will be took care ARC. But clearing the allocated arrays as in below code is a best practice.
- (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;
}