2011-05-30 16 views
3

我有一個包含日記部分名稱的文章數據庫。一篇是「應用物理雜誌」,另一篇是「應用物理雜誌」。當我使用在索引501處的獲取對象具有亂序部分名稱'JOURNAL OF APPLIED PHYSICS。對象必須按部分名稱排序'

[[NSSortDescriptor alloc] initWithKey:@"Journal" ascending:YES elector:@selector(caseInsensitiveCompare:)] 

來獲取數據,它給了我錯誤消息。

The fetched object at index 501 has an out of order section name 'JOURNAL OF APPLIED PHYSICS. Objects must be sorted by section name' 

我已經在使用不區分大小寫的比較,所以爲什麼這不起作用?幫幫我?

=======用於獲取數據======= 代碼

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Article" 
              inManagedObjectContext:SharedMOC]; 

[fetchRequest setEntity:entity]; 

NSSortDescriptor *journalSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Journal" 
                    ascending:ascending 
                     selector:@selector(caseInsensitiveCompare:)]; 

NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:journalSortDescriptor, nil]; 

[fetchRequest setSortDescriptors:sortDescriptors]; 

NSFetchedResultsController *a = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                    managedObjectContext:SharedMOC 
                     sectionNameKeyPath:[self selectedSortSection] 
                       cacheName:cacheName]; 
+0

Q1:你如何創建你的獲取請求? (請輸入代碼) Q2:是「@」日記「」您正在排序的屬性的名稱? – octy 2011-05-30 20:42:41

+0

見上文。 Q2:是的 – Dan 2011-05-30 20:57:49

+0

謝謝。在那種情況下,'Journal'是一個NSString?如果'Journal'是另一個實體,則例如,您的keypath應該是'Journal.name'。 – octy 2011-05-30 20:59:29

回答

1

我的直覺說,而不是:

[fetchRequest setSortDescriptors:[self getSortDescriptor]]; 

你應該寫:

[fetchRequest setSortDescriptors:sortDescriptors]; 
+0

+1這幾乎肯定是問題所在。 – TechZen 2011-05-30 22:47:11

+0

對不起。我確實有一個[self getSortDescriptor]方法,它只是在發佈時修改了代碼以簡化代碼,並錯誤地將這些代碼留在了未修改的位置。就像我在評論中所說的那樣,排序可以很好地與其他關鍵路徑(如作者,年份等)一起工作。就像Journal這種大小寫差異的情況一樣。 – Dan 2011-05-31 01:18:50

+0

好的。你是否使用NSFetchedResultsController中的'sectionIndexTitles',或者你是否使用子類來覆蓋該方法?從[doc](http://j.mp/kymRSJ):如果要自定義節和索引標題的創建,可以創建此類的子類。您可以覆蓋sectionIndexTitleForSectionName:如果您希望節索引標題不是節名稱的首字母大寫。如果希望索引標題不是通過調用sectionIndexTitleForSectionName所創建的數組之外的某個部分,則覆蓋sectionIndexTitles:在所有已知部分上。 – octy 2011-05-31 02:36:54