NSPredicate for @[@{@"someKey" : @"key"}] 搜索 someKey

NSPredicate for @[@{@"someKey" : @"key"}] search for someKey

[
    {"theKeyOne"  :"One"},
    {"theKeyTwo"  :"Two"},
    {"theKeyThree":"Three"},
],

我想将 String 用于 NSPredicate 的 predicateWithFormat

例如

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%@ == %@)", @"theKeyTwo", @"theKeyTwo"];

代码,我觉得只能输出{"theKeyTwo" :"Two"}

但输出allInfo。

第一个索引是{"theKeyOne" :"One"}!!!

我想 Two theKeyTwo

我只知道

theKeyTwo

我应该如何使用它 NSPredicate

问题不是很清楚。请选择我的变体之一:

  1. 如果要替换谓词中的键,则应使用 %K。所以您的谓词将如下所示:

    [NSPredicate predicateWithFormat:@"%K = %@", @"theKeyTwo", @"Two"];

输出是一个数组:@[@{@"theKeyTwo":@"Two"}]

  1. 要仅获取您应该使用的值 valueForKeypath:

    NSArray *result = [yourArray valueForKeypath:@"theKeyTwo"]

输出是一个数组:@[@"Two"]

好吧,试试这个:)

    NSMutableDictionary *object1 = [[NSMutableDictionary alloc] init];
    [object1 setValue: @"One" forKey: @"theKeyOne"];
    [object1 setValue: @"Two" forKey: @"theKeyTwo"];
    [object1 setValue: @"Three" forKey: @"theKeyThree"];
    [object1 setValue: @"Four" forKey: @"theKeyFour"];

    // Getting the value using the key
    NSString *valueOfKey = [object1 valueForKey: @"theKeyTwo"];

    // Making the predicate using the value of key string
    NSPredicate *pred2 = [NSPredicate predicateWithFormat: @"theKeyTwo MATCHES[c] %@", valueOfKey];