2011-10-07 24 views
0

我很喜歡簡單的基於iphone的apllication。在該應用程序coredata是用於存儲數據。顯示排序coredata與日期

我有兩個字段。 一個字段是Mesage,其他字段是日期。我想要在最近10天內發送的消息。 我知道這個代碼是用於那..但如何實現這?

sortedArray = [unsortedArray sortedArrayUsingFunction:sortByStringDate context:NULL] 

如何編寫一個查詢以及如何實現這個...任何人幫助我實現這個.....先感謝

回答

2

做這樣的事情:

// Calculate NSDate for 10 days ago 
NSDate *now = [NSDate date]; // get current date 
NSCalendar *calendar = [NSCalendar currentCalendar]; 
NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; 
dateComponents.day = -10; 
NSDate *tenDaysAgo = [calendar dateByAddingComponents:dateComponents toDate:now options:0]; 
[dateComponents release]; 

// Create NSFetchRequest 
NSFetchRequest *req = [[NSFetchRequest alloc] initWithEntityName:@"MyEntity"]; 
req.predicate = [NSPredicate predicateWithFormat:@"date >= %@", tenDaysAgo]; 
// Optionally add a sort descriptor too 

// Execute fetch request 
NSError *error = nil; 
NSArray *results = [managedObjectContext executeFetchRequest:req error:&error]; 

如果這對你沒有意義,那麼請閱讀Core Data Programming Guide中的NSFetchRequests和Predicates。

+0

非常感謝....................那很好工作 – sai

+0

非常感謝................... – sai

+2

現在發生的事情是,你點擊按鈕接受這個答案。 –

3

如果你想通過在物體的屬性date排序數組數組你可以寫這樣的電話:

sortedArray = [unsortedArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]]]; 
+0

k ....那麼如何識別過去30天? – sai

+0

創建新的獲取請求模板。提取對象時:加載並設置合適的日期。 – Nekto