简体   繁体   中英

filteredArrayUsingPredicate returning nothing

I'm building a application that I have to compare if an hour is between an inicial and a final hour that I'm saving like seconds in CoreData with the type Integer64. To compare this hours I'm using filteredArrayUsingPredicate with this predicate:

NSMutableArray *allDiscounts = (NSMutableArray *)[[DataStack defaultStack] fetchAllDiscounts];    
NSMutableArray *hoursResults = [[NSMutableArray alloc] init];    
    NSNumber *timeInSeconds = [[DataStack defaultStack] stringToSeconds:string];

NSLog(@"allDiscounts count: %lu",(unsigned long)[allDiscounts count]);
NSLog(@"inicial time; %@",[[allDiscounts objectAtIndex:0] inicialTime]);
NSLog(@"final time; %@",[[allDiscounts objectAtIndex:0] finalTime]);
NSLog(@"time in seconds: %d", [timeInSeconds intValue]);

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"inicialTime <= %@ AND %@ <= finalTime",timeInSeconds,timeInSeconds];
[hoursResults addObjectsFromArray:[allDiscounts filteredArrayUsingPredicate:predicate]];
NSLog(@"hoursResults count: %lu", (unsigned long)[discountResults count]);

and adding the result to another NSMutableArray, but I always getting 0 with result.

this is the log that i'm getting:

2015-03-15 16:05:09.883 Vanttage[9342:662093] allDiscounts count: 87
2015-03-15 16:05:09.883 Vanttage[9342:662093] inicial time; 68400
2015-03-15 16:05:09.883 Vanttage[9342:662093] final time; 82800
2015-03-15 16:05:09.884 Vanttage[9342:662093] time in seconds: 72000
2015-03-15 16:05:09.885 Vanttage[9342:662093] hoursResults count: 0

What I'm doing wrong? Hope you can help me.Thanks.

Try NSPredicate *predicate = [NSPredicate predicateWithFormat:@"inicialTime <= %d AND %d <= finalTime",[timeInSeconds intValue],[timeInSeconds intValue]]; assuming timeInSeconds is an NSNumber

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM