2011-06-07 24 views
2

我正在iphone應用中實施搜索欄。我有工人名單,每個都有屬性:名字,姓氏,公司。表格視圖的部分設置爲公司屬性。NSFetchedResultsController錯誤:在索引X處的抓取對象的亂序部分名稱爲Y.對象必須按部分名稱排序'

搜索時我沒設置斷言:

NSPredicate *predicate =[NSPredicate predicateWithFormat:@"firstName contains[cd] %@", searchBar.text]; 

,我得到錯誤:

NSFetchedResultsController ERROR: The fetched object at index 3 has an out of order section name 'company2. Objects must be sorted by section name' 

時,我有sortDescriptor:

,當我改變它
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES selector:@selector(caseInsensitiveCompare:)]; 

我沒有通知到

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName"` ascending:YES selector:@selector(caseInsensitiveCompare:)]; 

現在搜索正常工作,沒有錯誤。 initWithKey參數是否必須在謂詞中匹配屬性名稱?我不明白。

+0

你在使用NSFetchedResultsController嗎? – TechZen 2011-06-07 15:46:33

+0

是的,我正在使用NSFetchedResultsController。 – alhcr 2011-06-07 16:07:55

回答

13

從NSFetchedResultsController文檔:

The fetch request must have at least one sort descriptor. If the controller generates sections, the first sort descriptor in the array is used to group the objects into sections; its key must either be the same as sectionNameKeyPath or the relative ordering using its key must match that using sectionNameKeyPath.

在你的情況,你需要使用兩類元素之一應該是一種對公司名稱的屬性和第二元素應該排序的(在數組中。) lastName/firstName屬性。

+0

你是一個拯救生命的人!謝謝! – Jan 2013-10-07 17:43:10

相關問題