user1960279
user1960279

Reputation: 514

Integer Comparison through Predicate

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.

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.

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

Please help how to compare integer in NSPredicate.

Thanks in Advance.

Upvotes: 4

Views: 1309

Answers (2)

Kamil Harasimowicz
Kamil Harasimowicz

Reputation: 4994

@sage444 your answer is what I have looking for hours. Works like a charm.

I just provide Swift version for future readers:

let predicate = NSPredicate(format: "%K.stringValue CONTAINS[c] %@",
                            #keyPath(YOUR_MODEL.number),
                            substring)

Upvotes: 1

sage444
sage444

Reputation: 5684

Try cast integer to string and compare it

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"electronic_id.stringValue CONTAINS[c] %@",searchText];

Upvotes: 5

Related Questions