Getting constantValue: unrecognized selector sent to instance error
Getting this error with my fetch's predicate
2011-08-13 13:49:12.405 Codes[16957:10d03] NSlog Array: code BETWEEN {"06", "07"}
2011-08-13 13:49:12.407 Codes[16957:10d03] -[NSCFString constantValue]: unrecognized selector sent to instance 0x7464610
2011-08-13 13:49:12.409 Codes[16957:10d03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString constantValue]: unrecognized selector sent to instance 0x7464610'
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"ANY code BETWEEN %@", [NSArray arrayWithObjects:self.predicateFilterStart, self.predicateFilterEnd, nil]];:
NSLog shows code BETWEEN {"06", "07"}
Model Class for NSManagedObject with property code
:
@interface MedicalCode : NSManagedObject
@property (nonatomic, retain) NSString * code;
@property (nonatomic, retain) NSString * codeDescription;
@property (nonatomic, retain) NSString * name;
FRC method: (ICD9Procedure is subclass of MedicalCode)
- (NSFetchedResultsController *)fetchedResultsController
{
if (__fetchedResultsController != nil)
{
return __fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"ICD9Disease" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entit开发者_开发技巧y];
NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"ANY code BEGINSWITH[c] %@", self.predicateFilter];
[fetchRequest setPredicate:myPredicate];
[fetchRequest setFetchBatchSize:20];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"code" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return __fetchedResultsController;
}
EDIT:
All I need is a predicate that filters data.
I have a property of an entity called code that is in format like 55.534
.
I'm trying to fetch all data in a range, for example 50-60.
Try:
NSString *start = @"06";
NSString *end = @"07";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"ANY code BETWEEN %@",
[NSArray arrayWithObjects: start, end, nil]];
NSLog(@"%@", pred);
That works for me:
2011-08-13 23:45:52.019 PredicateTest[18493:707] ANY code BETWEEN {"06", "07"}
so the error is probably in the properties.
FWIW, constantValue
is a method of NSExpression. Not sure if that helps you.
I see you updated your code. Now it is very unclear where your error happened, since the code you orginally posted does not appear in the code you added, nor does it give additional info about the properties you are using.
And you say the error does not appear in the code you originally posted. It happens in a different piece of code, which you posted now, but it is not clear to me how that relates to the original code.
Update
Apparently, from the discussion inthe comments, it turns out you need numbers, so do something like:
NSPredicate *pred = [NSPredicate predicateWithFormat:@"ANY code BETWEEN %@",
[NSArray arrayWithObjects:
[NSNumber numberWithDouble: 50.0],
[NSNumber numberWithDouble: 60.0],
nil]];
Final solution
For those interested: the following apparently works.
NSPredicate *pred = [NSPredicate predicateWithFormat:@"ANY code BETWEEN %@",
[NSArray arrayWithObjects:
[NSExpression expressionForConstantValue: [NSNumber numberWithDouble: 50.0]],
[NSExpression expressionForConstantValue: [NSNumber numberWithDouble: 60.0]],
nil]];
精彩评论