NSpredicate与日期

时间:2014-02-14 09:04:09

标签: ios nspredicate

我有这个问题:

我想使用NSMutableArray过滤NSPredicate,但会返回此错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0xfc7f950'

我的代码:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];

NSPredicate *pred = [NSPredicate predicateWithFormat:[NSString stringWithFormat: @"dataGiorno >= '%@'",[dateFormatter dateFromString:@"31-12-2013"]]];

NSArray *filtered = [lista filteredArrayUsingPredicate:pred];

我的数据库行:

Row DB

谢谢!

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc ] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate* firstDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@ 00:00:00",@"2013-01-01" ]];
NSDate* secondDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@ 23:59:59",@"2013-12-31" ]];
NSPredicate *firstPredicate = [NSPredicate predicateWithFormat:@"dataGiorno >= %@", firstDate];
NSPredicate *secondPredicate = [NSPredicate predicateWithFormat:@"dataGiorno <= %@", secondDate];
NSPredicate *newCondition = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:firstPredicate, secondPredicate,nil]];
NSArray *filtered = [lista filteredArrayUsingPredicate:newCondition];
[dateFormatter release];

全部谢谢

1 个答案:

答案 0 :(得分:1)

Juste取代这一行:

NSPredicate *pred = [NSPredicate predicateWithFormat:[NSString stringWithFormat: @"dataGiorno >= '%@'",[dateFormatter dateFromString:@"31-12-2013"]]];

有了这个:

NSPredicate *pred = [NSPredicate predicateWithFormat:@"dataGiorno >= '%@'",[dateFormatter dateFromString:@"31-12-2013"]];

此处无需使用额外的NSString。