• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Trouble using ANY operator in an NSArrayController search predicate
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Trouble using ANY operator in an NSArrayController search predicate


  • Subject: Re: Trouble using ANY operator in an NSArrayController search predicate
  • From: Greg Herlihy <email@hidden>
  • Date: Sat, 20 May 2006 00:56:32 -0700
  • Thread-topic: Trouble using ANY operator in an NSArrayController search predicate

The problem is apparently that CONTAINS is not a string operator in
predicate comparisons. According to the BNF section of the Predicates
Programming Guide the four string operators are BEGINSWITH, ENDSWITH, LIKE
and MATCHES. So it looks like the Predicates Programming Guide mistakenly
includes CONTAINS (which is apparently an array operator) as a string
operator in its "Predicate String Format Syntax" section.

Anyway, I have verified that the following predicate successfully matches
the partial course name ("phy") against the full course name ("physics")
stored in the dictionary.

    NSPredicate *predicate = [NSPredicate predicateWithFormat: @"ANY
courses.name LIKE[cd] %@", @"*phy*"];

Greg

On 5/19/06 5:55 PM, "Jim Correia" <email@hidden> wrote:

>> On May 19, 2006, at 5:35 PM, Greg Herlihy wrote:
>> I'm not sure how the program constructs the current NSPredicate to
>> filter by
>> course name, but I would expect that the code would look like:
>>
>> [NSPredicate predicateWithFormat:@"courses.name CONTAINS[cd] %@",
>> courseName];
>>
>> wherein courseName is an NSString specifying the full or partial
>> text of the
>> course name to match.
>
> I tried that, and it is acting like it is doing an equality test, not
> a contains test.
>
> If you run the code below, the first test prints matches, the second
> test prints doesn't match. Am I doing something obviously wrong, or
> have I run into a bug?
>
> NSMutableArray *courses = [NSMutableArray array];
> [courses addObject: [NSDictionary dictionaryWithObject: @"physics"
> forKey: @"name"]];
> [courses addObject: [NSDictionary dictionaryWithObject: @"chemistry"
> forKey: @"name"]];
>
> NSDictionary *object = [NSDictionary dictionaryWithObject: courses
> forKey: @"courses"];
> NSPredicate *predicate = [NSPredicate predicateWithFormat:
> @"courses.name CONTAINS[cd] %@", @"physics"];
> if ([predicate evaluateWithObject: object])
> NSLog(@"matches");
> else
> NSLog(@"does not match");
>
> predicate = [NSPredicate predicateWithFormat: @"courses.name CONTAINS
> [cd] %@", @"phy"];
> if ([predicate evaluateWithObject: object])
> NSLog(@"matches");
> else
> NSLog(@"does not match");
>
>
> Jim


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Trouble using ANY operator in an NSArrayController search predicate
      • From: Jim Correia <email@hidden>
References: 
 >Re: Trouble using ANY operator in an NSArrayController search predicate (From: Jim Correia <email@hidden>)

  • Prev by Date: Re: Bindings, CoreData, NSOutlineView, and NSTreeController
  • Next by Date: Re: Possible to customize the toolbar customization sheet?
  • Previous by thread: Re: Trouble using ANY operator in an NSArrayController search predicate
  • Next by thread: Re: Trouble using ANY operator in an NSArrayController search predicate
  • Index(es):
    • Date
    • Thread