How to use NSMutableArray's filteredArrayUsingPredicate

  • I've got a NSMutableArray filled with NSDictionarys with two keys
    (@"Abbreviation" & @"Index" ) whose values are NSString's.

    What I would like to do is be able to use filteredArrayUsingPredicate
    on this array and have a new array containing only those elements
    where Abbreviation was 'Po'.

    I assume this is possible and relatively easy if you already know the
    answer...however, I do not and was hoping someone could tell me how to
    accomplish this.

    Thank you.
  • Well, this was easier then I thought.

    Unless there is something about this solution that does not work, the
    answer appears to be:

    NSArray    *filteredArray = [myArray filteredArrayUsingPredicate:
    [NSPredicate predicateWithFormat:[NSString
    stringWithFormat:@"Abbreviation LIKE \"%@\"", lookFor]]];

    If there are any other interesting, I would be interested.
    If there is any wrong with this one, I would be interested.

    On Dec 18, 2007, at 10:46 PM, Eric Gorr wrote:

    > I've got a NSMutableArray filled with NSDictionary's with two keys
    > (@"Abbreviation" & @"Index" ) whose values are NSString's.
    >
    > What I would like to do is be able to use
    > filteredArrayUsingPredicate on this array and have a new array
    > containing only those elements where Abbreviation was 'Po'.
    >
    > I assume this is possible and relatively easy if you already know
    > the answer...however, I do not and was hoping someone could tell me
    > how to accomplish this.
  • On Dec 18, 2007 10:16 PM, Eric Gorr <mailist...> wrote:
    > Unless there is something about this solution that does not work, the
    > answer appears to be:
    >
    > NSArray *filteredArray = [myArray filteredArrayUsingPredicate:
    > [NSPredicate predicateWithFormat:[NSString
    > stringWithFormat:@"Abbreviation LIKE \"%@\"", lookFor]]];
    >
    > If there are any other interesting, I would be interested.
    > If there is any wrong with this one, I would be interested.

    You misunderstood the "WithFormat:" part:

    <http://developer.apple.com/documentation/Cocoa/Conceptual/Predicates/Articl
    es/pSyntax.html#//apple_ref/doc/uid/TP40001795
    >

    For example:

    [NSPredicate predicateWithFormat:@"Abbreviation LIKE %@", lookFor]
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today