Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 994
Paul Peelen Avatar asked Apr 05 '13 10:04

Paul Peelen


2 Answers

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!

like image 177
Paul Peelen Avatar answered Oct 13 '22 00:10

Paul Peelen


LIKE[cd] will also do it

NSArray *filtered = [data filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(DisplayName LIKE[cd] %@)", @"purchaseAmount"]];

returned

<NSArray>(
    {
       DisplayName = PurchaseAmount;
       InternaName = "Number 1";
       NumberValue = 3500;
    }
)
like image 39
lindinax Avatar answered Oct 13 '22 00:10

lindinax