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

发布于 2024-11-29 17:20:21 字数 2690 浏览 0 评论 0原文

使用我的 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"}

NSManagedObject 的模型类,具有属性 code

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

编辑:

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手心的海 2024-12-06 17:20:21

尝试:

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

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]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文