如何从具有键特定值的字典数组中提取字典?

时间:2014-03-10 13:38:05

标签: ios objective-c nsarray nsdictionary

我有以下数组:

myArrey = (
    object1:{key1:valueA, key2:valueB},
    object2:{key1:valueC, key2:valueD}
)

我需要做些什么才能找到一个对象,即key1的值是keyC?

MyObject obj = [myArrey abrakadabra with: key1 and:valueC];
NSLog(@"obj - %@",obj);

obj = object2

1 个答案:

答案 0 :(得分:1)

使用indexOfObjectPassingTest:

NSInteger indexOfMatchingDictionary = [myArray indexOfObjectPassingTest:^BOOL(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
        return [[obj valueForKey:@"key1"] isEqual:@"valueC"];
}
NSDictionary *matchingDictionary = [myArray objectAtIndex:indexOfMatchingDictionary];