如何过滤NSDictionarys和NSDictionarys的数组?

时间:2012-11-06 11:58:58

标签: objective-c cocoa nsdictionary nspredicate

我有一个数组,其中包含几个带有键的 NSDictionaries

  • NSString *name
  • NSString *ID
  • NSDictionary *phoneNumbersDict

现在我想过滤此数组,找到phoneNumbersDict@"keyIAmLookingFor"和值@"valueIAmLookingFor"的数据。

NSMutableArray *addressBookPhoneIndividuals = [NSMutableArray array];
for (int i = 0; i < x; i++) {
    [addressBookPhoneIndividuals addObject:@{
        @"name" : @"...",
        @"ID" : @"...",
        @"phoneNumbers" : @{...}
    }];
}

4 个答案:

答案 0 :(得分:6)

NSString *keyIAmLookingFor = @"work";
NSString *valueIAmLookingFor = @"444-567-9019";

NSArray *addressBookPhoneIndividuals = @[
    @{
        @"name" : @"Mike Rowe",
        @"ID" : @"134",
        @"phoneNumbers" : @{
            @"work" : @"123-456-8000",
            @"school" : @"647-5543",
            @"home" : @"123-544-3321",
        }
    },
    @{
        @"name" : @"Eric Johnson",
        @"ID" : @"1867",
        @"phoneNumbers" : @{
            @"work" : @"444-567-9019",
            @"other" : @"143-555-6655",
        }
    },
    @{
        @"name" : @"Robot Nixon",
        @"ID" : @"-12",
        @"phoneNumbers" : @{
            @"work" : @"123-544-3321",
            @"school" : @"123-456-8000",
            @"home" : @"444-567-9019",
        }
    },
];

NSString *keyPath = [@"phoneNumbers." stringByAppendingString:keyIAmLookingFor];

NSPredicate *predicate =
[NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:keyPath]
                                   rightExpression:[NSExpression expressionForConstantValue:valueIAmLookingFor]
                                          modifier:NSDirectPredicateModifier
                                              type:NSEqualToPredicateOperatorType
                                           options:0];

NSArray *result = [addressBookPhoneIndividuals filteredArrayUsingPredicate:predicate];

这将返回一个包含“Eric Johnson”字典的数组。

我喜欢在做任何复杂匹配时推荐NSComparisonPredicate。查看修改器,类型和选项的选项。那里有一些很好的内置匹配引擎 - 包括正则表达式和不区分大小写。无论如何,这里可能没有必要,所以你可以替换以下内容:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K = %@", keyPath, valueIAmLookingFor];

如果您只关心第一个结果,则可以完全跳过keyPath /谓词业务:

for (NSDictionary *individualDict in addressBookPhoneIndividuals) {
    NSDictionary *phoneNumbers = [individualDict objectForKey:@"phoneNumbers"];
    NSString *possibleMatch = [phoneNumbers objectForKey:keyIAmLookingFor];
    if ([possibleMatch isEqualToString:valueIAmLookingFor]) {
        return individualDict;
    }
}

return nil;

答案 1 :(得分:5)

您可以使用NSPredicate过滤数组。如下所示:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"phoneNumbers.keyIAmLookingFor == %@", @"valueIAmLookingFor"];
NSArray *matches = [addressBookPhoneIndividuals filteredArrayUsingPredicate:predicate];

答案 2 :(得分:1)

我不太了解您希望如何过滤数组,但我经常使用的一种简单方法是基于块的过滤:

NSIndexSet* filteredIndexes = [addressBookPhoneIndividuals indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop){
    NSString *name = [obj objectForKey:@"name"];
    NSString *ID = [obj objectForKey:@"ID"];
    NSDictionary *phoneNumbersDict = [obj objectForKey:@"phoneNumbersDict"];

    return [[phoneNumbersDict objectForKey:@"keyIAmLookingFor"] isEqualToString:@"valueIAmLookingFor"];
}];

NSArray* filteredArray = [addressBookPhoneIndividuals objectsAtIndexes:filteredIndexes];

这有帮助吗?

答案 3 :(得分:0)

您可以使用

i.     –allKeysForObject:
ii.    –objectForKey:
iii.   –valueForKey:
iv.    –keysOfEntriesPassingTest:
v.     –keysOfEntriesWithOptions:passingTest:

你也可以去NSPredicateFilter。