NSArray充满了NSDictionaries。如何找到对象的索引?

时间:2012-09-13 10:58:40

标签: iphone objective-c ios xcode nsarray

我的array填充了NSDictionaries。我想查找其中一个字典的index,但我对该字典的了解仅为密钥value的{​​{1}} 我该怎么做 ?

2 个答案:

答案 0 :(得分:38)

查找theArray@"name"的值为theValue的第一个字典的索引:

NSUInteger index = [theArray indexOfObjectPassingTest:
        ^BOOL(NSDictionary *dict, NSUInteger idx, BOOL *stop)
        {
            return [[dict objectForKey:@"name"] isEqual:theValue];
        }
];
如果找不到匹配的对象,

index将为NSNotFound

答案 1 :(得分:1)

 NSArray *temp = [allList valueForKey:@"Name"];
 NSInteger indexValue = [temp indexOfObject:YourText];
 NSString *name = [[allList objectAtIndex:indexValue] valueForKey:@"Name"]