JSON , t21 . , , , - , :
if ([dict[key] isKindOfClass:[NSMutableArray class]]) {
NSMutableArray *values = (NSMutableArray *)dict[key];
[values addObject:newObject];
}
else {
NSMutableArray *values = [NSMutableArray arrayWithObject:dict[key]];
[values addObject:newObject];
}
, : (, JSON NSDictionary )
NSArray *events = @[@{@"start_time": @"17:00", @"end_time": @"17:30", @"english_event": @"Lion Dance", @"english_performer": @"Legendary Group"},
@{@"start_time": @"17:30", @"end_time": @"18:00", @"english_event": @"Singing", @"english_performer": @"Ivan Cheong"},
@{@"start_time": @"20:00", @"end_time": @"21:00", @"english_event": @"Music", @"english_performer": @"Adam Cease"},
@{@"start_time": @"20:00", @"end_time": @"21:00", @"english_event": @"Music", @"english_performer": @"DJ Happee From Channel 93.3"},
@{@"start_time": @"21:00", @"end_time": @"22:00", @"english_event": @"Singing", @"english_performer": @"Between California and Summer"}];
NSMutableDictionary *resultDict = [NSMutableDictionary dictionary];
[events enumerateObjectsUsingBlock:^(NSDictionary *event, NSUInteger idx, BOOL *stop) {
NSMutableArray *values = resultDict[event[@"start_time"]];
if (values == nil) {
values = [[NSMutableArray alloc] init];
resultDict[event[@"start_time"]] = values;
}
[values addObject:event];
}];
. , collection operators, KVO accessors .., 0(n) .
source
share