NSFetchedResultsController 错误:索引 X 处获取的对象有一个无序的节名称 Y。对象必须按节名称排序
我正在 iPhone 应用程序中实现搜索栏。我有工人列表,每个工人都有属性:名字、姓氏、公司。表视图的部分设置为公司属性。
我在搜索时确实设置了谓词:
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 参数是否必须与谓词中的属性名称匹配?我不明白。
I'm implementing search bar in iphone app. I have list of Workers, each have attributes: firstName, lastName, company. Sections of table view are set to company attribute.
I did set predicate when searching:
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"firstName contains[cd] %@", searchBar.text];
and i get error:
NSFetchedResultsController ERROR: The fetched object at index 3 has an out of order section name 'company2. Objects must be sorted by section name'
when I have sortDescriptor:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES selector:@selector(caseInsensitiveCompare:)];
I did notice that when I change it to
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName"` ascending:YES selector:@selector(caseInsensitiveCompare:)];
now search works correctly with no error. Does initWithKey param must match attribute name in predicate? I don't get it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 NSFetchedResultsController 文档:
在您的情况下,您需要使用两种排序(在数组中)。第一个元素应该根据公司名称属性进行排序,第二个元素应该根据lastName/firstName 属性进行排序。
From the NSFetchedResultsController docs:
In your case, you need to use two sorts (in an array.) Element one should be a sort on the company name attribute and second element should sort on the lastName/firstName attribute.