Re: Trouble using ANY operator in an NSArrayController search predicate
Re: Trouble using ANY operator in an NSArrayController search predicate
- Subject: Re: Trouble using ANY operator in an NSArrayController search predicate
- From: Jim Correia <email@hidden>
- Date: Fri, 19 May 2006 20:55:17 -0400
On May 19, 2006, at 5:35 PM, Greg Herlihy wrote:
The "Predicates Programming Guide" has a brief section on arrays and
filters:
http://developer.apple.com/documentation/Cocoa/Conceptual/
Predicates/Article
s/pUsing.html#//apple_ref/doc/uid/TP40001794-250409
I agree that this description could be clearer about what exactly
comprises
the "left side" of an expression being used in predicate to filter an
NSArray. Furthermore I can certainly see how the "contents" of the
array
could be interpreted as a reference to the array itself and not to the
individual items within. (Assuming that my interpretation is in fact
correct).
I read that and I'm still note sure what the left hand side must be
an array means. (And this works when the predicate is translated to
SQL for a Core Data SQLite store.)
Furthermore, if I take the exception at face value and test against
an array instead of a single object, the exception is still raised:
NSMutableArray *courses = [NSMutableArray array];
[courses addObject: [NSDictionary dictionaryWithObject: @"physics"
forKey: @"name"]];
[courses addObject: [NSDictionary dictionaryWithObject: @"chemistry"
forKey: @"name"]];
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:
courses forKey: @"courses"];
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"ANY
courses.name CONTAINS[cd] %@", @"physics"];
NSArray *array = [NSArray arrayWithObjects: dictionary, dictionary,
nil];
[predicate evaluateWithObject: array]; // raises exception
I'm probably being thick headed, but I'd sure like to be enlightened :-)
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