Search code examples
iosobjective-cnsarraynsdictionarynspredicate

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


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

I want use String for predicateWithFormat to NSPredicate

for example

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

the code, I think only output {"theKeyTwo" :"Two"}

but output allInfo.

the first Index is {"theKeyOne" :"One"} !!!

I want to get Two for theKeyTwo

I just know the

theKeyTwo

How should I use it for NSPredicate?


Solution

  • Well, try with this :)

        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];