获取constantValue:无法识别的选择器发送到实例错误

时间:2011-08-13 21:08:06

标签: iphone objective-c nsstring nspredicate fetch

使用我的fetch的谓词

获取此错误
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显示code BETWEEN {"06", "07"}

具有属性code的NSManagedObject的模型类:

@interface MedicalCode : NSManagedObject

@property (nonatomic, retain) NSString * code;
@property (nonatomic, retain) NSString * codeDescription;
@property (nonatomic, retain) NSString * name;

FRC方法:(ICD9Procedure是MedicalCode的子类)

- (NSFetchedResultsController *)fetchedResultsController
{
    if (__fetchedResultsController != nil)
    {
        return __fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"ICD9Disease" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    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;
} 

编辑:

我需要的只是一个过滤数据的谓词。 我有一个称为代码的实体的属性,其格式类似于55.534。 我正在尝试获取范围内的所有数据,例如50-60。

1 个答案:

答案 0 :(得分:3)

尝试:

NSString *start = @"06";
NSString *end = @"07";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"ANY code BETWEEN %@",
                      [NSArray arrayWithObjects: start, end, nil]];
NSLog(@"%@", pred);

这对我有用:

2011-08-13 23:45:52.019 PredicateTest[18493:707] ANY code BETWEEN {"06", "07"}

所以错误可能在属性中。

FWIW,constantValue是NSExpression的一种方法。不确定这对你有帮助。


我看到你更新了你的代码。现在很不清楚您的错误发生在哪里,因为您发布的代码没有出现在您添加的代码中,也没有提供有关您正在使用的属性的其他信息。

并且您说错误没有出现在您最初发布的代码中。它发生在您现在发布的不同代码中,但我不清楚它与原始代码的关系。

更新

显然,从评论中的讨论来看,结果证明你需要数字,所以做的事情如下:

NSPredicate *pred = [NSPredicate predicateWithFormat:@"ANY code BETWEEN %@",
                      [NSArray arrayWithObjects: 
                        [NSNumber numberWithDouble: 50.0],
                        [NSNumber numberWithDouble: 60.0],
                        nil]];

最终解决方案

对于那些感兴趣的人:以下显然是有效的。

NSPredicate *pred = [NSPredicate predicateWithFormat:@"ANY code BETWEEN %@",
                      [NSArray arrayWithObjects: 
                        [NSExpression expressionForConstantValue: [NSNumber numberWithDouble: 50.0]],
                        [NSExpression expressionForConstantValue: [NSNumber numberWithDouble: 60.0]],
                        nil]];