-2
我有這樣的問題: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];
我的數據庫列:
謝謝!
SOLUTION
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];
感謝所有
同樣的錯誤.. :(我找到了解決方案..現在我編輯問題...謝謝 – user3019841