Search code examples
iosobjective-cnsarraynsdictionary

Retrieve NSDictionary from NSArray where dictionary key has value X


I have an NSArray with NSDictionaries. One of the dictionaries keys in one of the arrays contains a value. I want to retrieve the NSDictionary with that value.

My array:

Array: (
        {
        DisplayName = "level";
        InternalName = "Number 2";
        NumberValue = 1;
    },
        {
        DisplayName = "PurchaseAmount";
        InternalName = "Number 1";
        NumberValue = 3500;
    }
)

So, I would like to get the dictionary which contains DisplayName set to PurchaseAmount (case insensitive).

How can I accomplish that?


Solution

  • The following solved my problem:

    NSArray *filtered = [promotions filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(DisplayName == %@)", @"PurchaseAmount"]];
    NSDictionary *item = [filtered objectAtIndex:0];
    

    Thnx to user Nate for his comment on my question!