如果面向iOS 4.0或更高版本,有NSArray
方法允许您使用块进行此操作。
– indexOfObjectPassingTest:
– indexesOfObjectsPassingTest:
等。
NSArray *test = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"A", @"Code", @"None", @"Comment", @"Core", @"Core", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"B", @"Code", @"None", @"Comment", @"Core", @"Core", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"C", @"Code", @"None", @"Comment", @"Core", @"Core", nil],
nil];
NSIndexSet *indexes =[test indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return [[obj valueForKey:@"Code"] isEqualToString:@"B"];
}];
NSLog(@"Indexes with Code B: %@", indexes);
来源
2011-09-27 19:46:43
Joe