我有这个问题:
我想使用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];
我的数据库行:
谢谢!
解
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];
全部谢谢
答案 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。