10

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?

Paul Peelen
  • 10,073
  • 15
  • 85
  • 168

5 Answers5

18

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!

Paul Peelen
  • 10,073
  • 15
  • 85
  • 168
  • 3
    Actually, both this answer, and [especially lindinax's answer](http://stackoverflow.com/a/15831638/119114) are better than the one you accepted. Bhargavi's answer checks that a key *contains* the string, but your question asked for a result with a key equal to a certain value. In general, those two aren't the same thing. It may not make a difference for your key names, but for others it very well could. – Nate Apr 05 '13 at 21:01
  • You are answering your own question wrong as you asked for "case insensitive". Especially as you set it as the accepted one. – lindinax Jan 17 '14 at 16:24
  • @lindinax fair enough. I think the reason I chose my answer before yours is mostly because I posted it before you posted yours. But, you are correct and I changed it now, 5 years later ;) – Paul Peelen Apr 11 '18 at 09:07
9

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;
    }
)
lindinax
  • 282
  • 2
  • 6
5

Use NSPredicate this way

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"DisplayName LIKE[cd] 'PurchaseAmount' "];
NSArray *filter = [array filteredArrayUsingPredicate:predicate];
NSLog(@"%@",filter);

Here filter will hold your dictionaries which contains DisplayName set to PurchaseAmount (case insensitive)

βhargavḯ
  • 9,786
  • 1
  • 37
  • 59
  • Nice! Thank you. Better then my newly found solution. – Paul Peelen Apr 05 '13 at 10:20
  • Actually, this is **not** what the question asked. Your predicate finds matches where the key **contains** the string. If there's another key that is a superset of this key name, your filter gives a *false positive*. (e.g. `DisplayName = "PurchaseAmountWithTax"`;) – Nate Apr 05 '13 at 20:58
  • You are right, even though Bhargavi's solution sure is usefull! – Paul Peelen Apr 09 '13 at 10:44
3

Answer in Swift,

let predicate = NSPredicate(format: "name contains[cd] %@", stringLookingFor)
let newList = data .filteredArrayUsingPredicate(predicate)

The data nsarray looks like this

[
 {
   name = Carlos,
   total = 9
 },
 {
   name = Willy,
   total = 2
 }
]
LuAndre
  • 1,114
  • 12
  • 23
0
  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"DisplayName == 'level'"];
  NSArray *arrOutput = [[Array filteredArrayUsingPredicate:predicate] mutableCopy];

with the help of above 2 lines you will get all dictionary with DisplayName = "PurchaseAmount"

Maheta Dhaval K
  • 764
  • 6
  • 19