7

我有一个数组,它由几个带有键的NSDictionaries组成:

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

现在我想过滤这个数组以找到具有phoneNumbersDictkey@"keyIAmLookingFor"和 value的数组@"valueIAmLookingFor"

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

4 回答 4

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/predicate 业务:

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

return nil;
于 2012-11-06T17:13:46.840 回答
5

您可以使用 anNSPredicate来过滤您的数组。类似于以下内容:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"phoneNumbers.keyIAmLookingFor == %@", @"valueIAmLookingFor"];
NSArray *matches = [addressBookPhoneIndividuals filteredArrayUsingPredicate:predicate];
于 2012-11-06T16:23:11.447 回答
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];

这有帮助吗?

于 2012-11-06T14:46:00.920 回答
0

您可以使用

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

你也可以选择 NSPredicateFilter。

于 2012-11-06T14:26:12.057 回答