Buggy NSPredicate?
Buggy NSPredicate?
- Subject: Buggy NSPredicate?
- From: Aurélien Hugelé <email@hidden>
- Date: Thu, 21 Sep 2006 17:37:53 +0200
Hi list!
Before reporting a bug on radar, i just want to be sure i did not
make any stupid mistake in my code. I'm pretty sure to have found a
major bug in NSPredicate but i would like someone to confirm this
I've joined a sample code that you can build very quickly.
Basically, i'm creating NSArrays of NSPredicates and during their
creation then addition to the array, the predicates already added in
the array are "corrupted"...
#import <Foundation/Foundation.h>
/*
what we should obtain at the end of the function:
predicates array should contains 2 predicates:
{ bar IN className AND foo IN className ,
bar IN description AND foo IN description }
what we DO obtain at the end of the function:
predicates array contains 2 equal (in the sense of isEqual:)
predicates:
{ bar IN description AND foo IN description ,
bar IN description AND foo IN description }
Note how does the first object of the predicates array has been
"corrupted".
The only way to avoid the problem is to recreate the subPredicates
mutable array for each criteria.
WTF guys? :)
*/
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableArray* patterns = [NSMutableArray
arrayWithObjects:@"foo",@"bar",nil];
NSMutableArray* predicates = [NSMutableArray array];
NSMutableArray* subPredicates = [NSMutableArray array];
NSLog(@"predicates = %@",predicates);
NSLog(@"subPredicates = %@",subPredicates);
// first critera
unsigned int i = [patterns count];
while(i--)
{
NSPredicate* subPredicate = [NSPredicate
predicateWithFormat:@"className CONTAINS %@",[patterns
objectAtIndex:i]];
if(subPredicate)
[subPredicates addObject:subPredicate];
}
[predicates addObject:[NSCompoundPredicate
andPredicateWithSubpredicates:subPredicates]];
NSLog(@"predicates = %@",predicates);
NSLog(@"subPredicates = %@",subPredicates);
// second critera
[subPredicates removeAllObjects];
// NSLog(@"predicates = %@",predicates); UNCOMMENT HERE AND YOU'LL
SEE A VERY STRANGE EXCEPTION HAPPENING!
i = [patterns count];
while(i--)
{
NSPredicate* subPredicate = [NSPredicate
predicateWithFormat:@"description CONTAINS %@",[patterns
objectAtIndex:i]];
if(subPredicate)
[subPredicates addObject:subPredicate];
}
[predicates addObject:[NSCompoundPredicate
andPredicateWithSubpredicates:subPredicates]];
NSLog(@"predicates = %@",predicates); // -> note here how
[predicates objectAtIndex:0] has been corrupted/modified !?
NSLog(@"subPredicates = %@",subPredicates);
[pool release];
return 0;
}
_______________________________________________
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