0

例如,如果我有这个数组:

_mode = [NSArray arrayWithObjects:
                      [NSArray arrayWithObjects:@"1", @"One", nil],
                      [NSArray arrayWithObjects:@"2", @"Two", nil],
                      [NSArray arrayWithObjects:@"3", @"Three", nil],
                      [NSArray arrayWithObjects:@"4", @"Four", nil],
                      [NSArray arrayWithObjects:@"5", @"Five", nil],
                      nil];

我需要找到数组 _mode 的哪个对象包含@"3"我该怎么做?我已经尝试过selectedIndex = [_mode indexOfObject:@"3"];selectedIndex = [[_mode objectAtIndex:0] indexOfObject:@"sta"];但都没有工作。

4

4 回答 4

9
[_mode indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop){
    return [[obj objectAtIndex:0] isEqualToString:@"3"];
}];
于 2012-04-08T17:46:04.037 回答
3

这与 Jonas Schnelli 的回答基本相同 :)

for (NSArray *subarray in _mode){
    NSInteger index = [subarray indexOfObject:@"3"];
    if (index != NSNotFound) return subarray;
}
于 2012-04-08T17:50:37.120 回答
2

这给出了数组中字典的索引。

for (NSDictionary *dict in self.yourArray ) {
    if ([anotherArray containsObject:[dict valueForKey:@"Key"]]) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.yourArray indexOfObject:dict] inSection:section];
        NSLog(@"IndexPath is %@",indexPath);
    }    
}
于 2014-11-22T11:01:27.530 回答
2

像这样?

for(NSArray *subarray in _mode) {
  for(NSString *str in subarray) {
   if([str isEqualToString:@"3"]) {
     return subarray; // returns the array within _mode that contains "3"
   }
  }
}

如果您需要索引:

for(NSArray *subarray in _mode) {
  for(NSString *str in subarray) {
   if([str isEqualToString:@"3"]) {
     return [mode indexOfObject:subarray];
   }
  }
}
于 2012-04-08T17:38:35.950 回答