问题描述
我使用的核心数据中有一个名为扫描" 的实体,该实体具有一个名为 electronic_id 的属性,该属性属于NSNumber
类型.现在,假设在数据库中,我有3个 Scan 属性条目.
I am using core data in which I am having an entity called "Scan" who is having one attribute say electronic_id which is of NSNumber
type. Now suppose in data base i am having 3 entries of this Scan attribute.
- 该实体的一个条目,其
electronic_id
为35. - 该实体的第二个条目,具有354作为
electronic_id
. - 该实体的第三项,其
electronic_id
为375.
- One entry of this entity having 35 as
electronic_id
. - Second entry of this entity having 354 as
electronic_id
. - Third entry of this entity having 375 as
electronic_id
.
我正在对此属性进行搜索.所以当我开始通过传递3来搜索实体时,我想要所有这3个对象.当我传递35时,我想要前两个对象.
I am performing searching on this attribute. so I when i start to search entity by passing 3 i want all these 3 objects.when i pass 35 then i want first two objects.
我想通过谓词对NSNumber
上的对象进行部分匹配.我正在使用此功能.
I want partial matches objects on NSNumber
through predicate.I am using this function.
-(void)filteredContentWithSearchText:(NSString*)searchText
{
if([searchText length])
{
int value=0;
value=[searchText intValue];
if ([strSelectedType isEqualToString:@"Electronic ID"])
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"electronic_id == %d",value];
NSManagedObjectContext *context =[self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Scan" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:predicate];
NSError *error=nil;
NSArray *results = [context executeFetchRequest:fetchRequest error:&error];
if (error==nil)
{
}
else if (error!=nil)
{
NSArray *results =[APP_DELEGATE CallForGetScan:YES Predicateis:predicate];
[arrSessionSearchData removeAllObjects];
[arrSessionSearchData addObjectsFromArray:results];
}
}
}
}
请帮助如何比较NSPredicate
中的整数.
Please help how to compare integer in NSPredicate
.
在此先感谢.
推荐答案
尝试将整数转换为字符串并进行比较
Try cast integer to string and compare it
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"electronic_id.stringValue CONTAINS[c] %@",searchText];
这篇关于通过谓词进行整数比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!