NSPredicate and square brackets

  • How can I get NSPredicate to properly recognize square brackets?

    NSString *item = @"[unknown]";

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name MATCHES %@",
    item];

    nameArray = [nameArray filteredArrayUsingPredicate:predicate]; // filtered
    array does not include object @"[unknown]"
  • Found the solution:

    NSString *value = [NSString stringWithFormat:@"name MATCHES '%@'", item];

    NSPredicate *predicate = [NSPredicate predicateWithValue:value];

    On Nov 29, 2007 3:28 AM, <slasktrattenator...> wrote:

    > How can I get NSPredicate to properly recognize square brackets?
    >
    >
    > NSString *item = @"[unknown]";
    >
    > NSPredicate *predicate = [NSPredicate predicateWithFormat: @"name MATCHES
    > %@", item];
    >
    > nameArray = [nameArray filteredArrayUsingPredicate :predicate]; //
    > filtered array does not include object @"[unknown]"
    >
  • Thank you, that worked. I should have read up on the syntax before posting.

    On Nov 29, 2007 5:00 PM, $B!g(B <me...> wrote:

    >
    > Il giorno 29/nov/07, alle ore 14:17, <slasktrattenator...> ha
    > scritto:
    >
    >> NSString *value = [NSString stringWithFormat:@"name MATCHES '%@'",
    >> item];
    >>
    >> NSPredicate *predicate = [NSPredicate predicateWithValue:value];
    >
    > The compiler warning might have tipped off the fact you are doing
    > something completely wrong.
    > +predicateWithValue's signature is:
    >
    > + (id) predicateWithValue:(BOOL) value;
    >
    > and produces a predicate which is always true (YES) or always false
    > (NO). You are passing a nonzero pointer, which is cast to an integer
    > (with an irate warning!), and works like YES since it is nonzero.
    >
    > Also:
    >
    >> NSString *item = @"[unknown]";
    >>
    >> NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name
    >> MATCHES %@",
    >> item];
    >>
    >> nameArray = [nameArray filteredArrayUsingPredicate:predicate]; //
    >> filtered
    >> array does not include object @"[unknown]"
    >
    > Of course it doesn't, because [@"unknown" name] is not @"unknown". You
    > probably want self rather than name, and you probably don't want to
    > use MATCHES since it requires a regular expression (and [] are special
    > characters in a regular expression).
    >
    > Look at the Predicate Programming Guide, especially
    > <file:///Developer/Documentation/DocSets/com.apple.ADC_Reference_Library.CoreReference.docset/Contents/Resources/Documents/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html
    >> for the predicate syntax.
    >
    > In this particular case, you probably want:
    >
    > NSPredicate* p = [NSPredicate predicateWithFormat:@"self == %@", item];
    >
    > - $B!g(B
    >
    >
previous month november 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    
Go to today