NSPredicate help (String value in NSArray?)

  • Hi all:

    I need some help with NSPredicate. Two objects are relevant in that
    context:

    (1) NSArray authors (that is an Array of NSString objects)
    (2) NSString [self wikiID] (returns an NSString object)

    I want to see if the string value returned by [self wikiID] is in
    NSArray authors:

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    NSPredicate *containsPredicate = [NSPredicate predicateWithFormat:@"SELF IN %@", authors];
    if ([containsPredicate evaluateWithObject:[self wikiID]]) {
        NSLog(@"YES");
    } else {
        NSLog(@"NO");
    }
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    No success, even if the string value can be found in the array.
    (Possibly because it does not compare the string values but their IDs
    instead.)

    How do I correctly use NSPredicate to check if a given string values is
    in a given array?

    Thanks for your hints.

    Cheers,
    /CA
  • On Nov 26, 2007, at 4:09 AM, Claus Atzenbeck wrote:

    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    > NSPredicate *containsPredicate = [NSPredicate
    > predicateWithFormat:@"SELF IN %@", authors];
    > if ([containsPredicate evaluateWithObject:[self wikiID]]) {
    > NSLog(@"YES");
    > } else {
    > NSLog(@"NO");
    > }

    Hello,

    Try this...

    NSPredicate *containsAuthorPredicate = [NSPredicate
    predicateWithFormat:@"(SELF like %@)", [self wikiID]];
    NSArray *validAuthors = [authors filteredArrayUsingPredicate:
    containsAuthorPredicate];

    if([validAuthors count] > 0)
    {
    NSLog(@"Yes");
    } else {
    NSLog(@"No");
    }

    hth,

    Jaime
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