Re: executeFetchRequest with BETWEEN
Re: executeFetchRequest with BETWEEN
- Subject: Re: executeFetchRequest with BETWEEN
- From: "Gerriet M. Denkmann" <email@hidden>
- Date: Sun, 14 Feb 2010 15:30:36 +0700
> I would like to get some entities (in iPhone 3.1.3). So I do:
>
> NSNumber *sta = ...
> NSLog(@" sta %@ %@ %p", sta, [sta class],sta); // sta 6 NSCFNumber 0x3b3a690
> NSNumber *las = ...
>
> NSArray *limits = [ [ NSArray alloc ] initWithObjects: sta, las, nil ];
> predicate = [ NSPredicate predicateWithFormat: @"%K between %@", @"windex", limits ];
> [ limits release ];
>
> [ reques2 setPredicate: predicate ];
> NSSortDescriptor *sd = [ [ NSSortDescriptor alloc ] initWithKey: @"windex" ascending: YES ];
> NSArray *sortDescriptors = [ [ NSArray alloc ] initWithObjects: sd, nil ];
> [ sd release ];
> [ reques2 setSortDescriptors:(NSArray *)sortDescriptors ];
> [ sortDescriptors release ];
>
> NSLog(@" will executeFetchRequest %@", reques2);
> // will executeFetchRequest <NSFetchRequest: 0x3d54c90> (entity: DictWord; predicate: (windex BETWEEN {6, 4842}); sortDescriptors: (
> (windex, ascending, compare:)
> ); limit: 0)
>
> // Note: windex is an int32 predicate of the entity DictWord
>
> NSError *outError;
> NSArray *results = [ moc executeFetchRequest: reques2 error: &outError ];
>
> But instead of some nice results I get:
> *** -[NSCFNumber constantValue]: unrecognized selector sent to instance 0x3b3a690
>
> Note: constantValue is a message understood by NSExpression (not by NSNumber)
> and 0x3b3a690 is my NSNumber sta (see above).
>
> It seems that my predicate is wrong, but how to make it right?
It seems the "Predicates Programming Guide" did confuse me (not sure who is to blame for this comfusion - could be easily be me).
The chapter "Predicate Format String Syntax" has this example:
NSNumber *one = [NSNumber numberWithInteger:1];
NSNumber *ten = [NSNumber numberWithInteger:10];
NSPredicate *betweenPredicate = [NSPredicate predicateWithFormat: @"attributeName BETWEEN %@", [NSArray arrayWithObjects:one, ten, nil]];
But maybe this (which now works for me) is a better way:
NSExpression *s1 = [ NSExpression expressionForConstantValue: sta ];
NSExpression *s2 = [ NSExpression expressionForConstantValue: las ];
NSArray *limits = [ NSArray arrayWithObjects: s1, s2, nil ];
predicate = [ NSPredicate predicateWithFormat: @"%K between %@", @"windex", limits ];
So it seems that the direct use of NSNumbers (instead of NSNumbers wrapped into an NSExpression) is not always such a good idea.
Kind regards,
Gerriet.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden