filteredArrayUsingPredicate什么都不返回

时间:2015-03-15 19:16:32

标签: ios objective-c core-data nspredicate

我正在构建一个我需要比较的应用程序,如果一小时介于一个小时和最后一小时之间,我在CoreData中使用Integer64类型保存了几秒钟。为了比较这个小时,我正在使用filteredArrayUsingPredicate和这个谓词:

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

并将结果添加到另一个NSMutableArray,但我总是得到0结果。

这是我得到的日志:

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

我做错了什么?希望你能帮助我。谢谢。

1 个答案:

答案 0 :(得分:0)

尝试NSPredicate *predicate = [NSPredicate predicateWithFormat:@"inicialTime <= %d AND %d <= finalTime",[timeInSeconds intValue],[timeInSeconds intValue]];假设timeInSeconds是NSNumber