2014-02-14 80 views
-2

我有這樣的問題:NSpredicate與日期

我想篩選NSMutableArrayNSPredicate,但它返回此錯誤:

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

謝謝!

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

感謝所有

回答

1

中庸之道替換此行:

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

有了這一個:

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

不需要在這裏使用額外的NSString。

+0

同樣的錯誤.. :(我找到了解決方案..現在我編輯問題...謝謝 – user3019841