そうする方法はたくさんありますが、そのうちのいくつかはここにあります..
A:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"period_id == %@", @"2"];
NSArray *newArray = [array filteredArrayUsingPredicate:predicate];
NSLog(@"%d", [newArray count]);
B:
NSMutableArray *newArray = [[NSMutableArray alloc] init];
for (id obj in array)
{
if([obj[@"period_id"] isEqualToString:@"2"]){
[newArray addObject:obj];
}
}
NSLog(@"%d", [newArray count]);
子:
NSArray *allIds = [array valueForKey:@"period_id"];
NSCountedSet *set = [[NSCountedSet alloc] initWithArray:allIds];
for (id item in set)
{
NSLog(@"period_id=%@, Count=%d", item,[set countForObject:item]);
}
D:
NSArray *allIds = [array valueForKey:@"period_id"];
__block NSMutableArray *newArray = [[NSMutableArray alloc] init];
NSString *valueToCheck = @"2";
[allIds enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if([obj isEqualToString:valueToCheck])
[newArray addObject:obj];
}];
NSLog(@"%d", [newArray count]);
E:
NSIndexSet *indexes = [array indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
return [[obj objectForKey:@"period_id"] isEqualToString:@"2"];
}];
NSArray *newArray = [array objectsAtIndexes:indexes];
NSLog(@"%d", [newArray count]);