• 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
Buggy NSPredicate?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Buggy NSPredicate?
      • From: Stephane Sudre <email@hidden>
  • Prev by Date: Designating UTIs for non-document files written by application
  • Next by Date: Re: How much does NSObject's bind:toObject:withKeyPath:options: do?
  • Previous by thread: Designating UTIs for non-document files written by application
  • Next by thread: Re: Buggy NSPredicate?
  • Index(es):
    • Date
    • Thread