我正在使用这个具有 timeStamp 属性的实体,该属性恰好是 Date 类型
当我尝试按日期执行搜索时,问题就出现了:
NSDate *today = [NSDate date];
NSDate *end_day = [today dateByAddingTimeInterval: -5*one_day]; // the last 5 days
result = [helper fetchObjectsForEntityName:@"MyEntity" withPredicate:
[NSPredicate predicateWithFormat:@"timestamp > %f", [end_day ] ]];
当我打印出谓词的值时,我得到的只是:
timestamp > CAST(378132723.717909, "NSDate")
但是当语句被执行时,它会崩溃。我在代码的其他部分使用相同的方法,它没有问题,显然如果我删除 NSPredicate,它又可以工作了。
为了这个问题,这里也是fetchObjectsForEnittyName: WithPredicate 的代码:
- (NSSet *)fetchObjectsForEntityName:(NSString *)newEntityName
withPredicate:(id)stringOrPredicate, ...
{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:newEntityName];
if (stringOrPredicate)
{
NSPredicate *predicate;
if ([stringOrPredicate isKindOfClass:[NSString class]])
{
va_list variadicArguments;
va_start(variadicArguments, stringOrPredicate);
predicate = [NSPredicate predicateWithFormat:stringOrPredicate
arguments:variadicArguments];
va_end(variadicArguments);
} else {
NSAssert2([stringOrPredicate isKindOfClass:[NSPredicate class]],
@"Second parameter passed to %s is of unexpected class %@",
sel_getName(_cmd), @"Whatevah");
predicate = (NSPredicate *)stringOrPredicate;
}
[request setPredicate:predicate];
}
NSError *error = nil;
NSArray *results = [self.managedObjectContext executeFetchRequest:request error:&error];
if (error != nil)
{
[NSException raise:NSGenericException format:[error description]];
}
return [NSSet setWithArray:results];
}
有没有人知道问题可能是什么?