2011-03-21 105 views
1

如何預測所有上週插入的數據,上個月昨天搜索按日期coredata

我在coredata NSDate的領域,但我不知道如何使用它

搜索任何建議或樣品做

問候

回答

2

我假設你有一個fetchedResultsController設置爲從數據庫中檢索對象。 當您設置fetchedResultsController時,您必須將NSPredicate添加到您的fetchRequest。

日期無恥地從here

//You can set up your custom dates like this 
NSDate *today = [NSDate date]; 

NSDate *yesterday = [today addTimeInterval: -86400.0]; 
NSDate *thisWeek = [today addTimeInterval: -604800.0]; 
NSDate *lastWeek = [today addTimeInterval: -1209600.0]; 

// This predicate retrieves all the object created since last week. 
NSpredicate *predicate = [NSPredicate predicateWithFormat:@"yourDateField >= %@ ", lastWeek]; 
[fetchRequest setPredicate:predicate];  

// You can add sorting like this 
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"yourDateField" ascending:YES]; 
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 

[fetchRequest setSortDescriptors:sortDescriptors]; 

希望這有助於被盜。如果您有任何問題,請告訴我。