我是IOS编程的新手,我希望得到你的帮助。
首先,我有一个表视图,其中包含NSMutablearray
“recs”的对象。
我插入了一个UISearchBar
,目的是从我想要的recs中找到每个对象。
当我尝试键入搜索栏时,它会崩溃并显示以下消息以记录文件
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't use in/contains operator with collection <Person: 0x8e20b10> (not a collection)'
我向您展示了包含NSPredicate的代码的一部分:
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
// Tells the table data source to reload when text changes
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
// Update the filtered array based on the search text and scope.
// Remove all objects from the filtered search array
[self.searcharray removeAllObjects];
// Filter the array using NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains[cd] %@",searchText];
_searcharray = [NSMutableArray arrayWithArray:[_recs filteredArrayUsingPredicate:predicate]];
}
我很困惑...... 请帮帮我!!!!
答案 0 :(得分:1)
错误消息表明您的数组包含Person
个对象,而不仅仅是
字符串。在这种情况下,您必须指定应该用于搜索的Person
类的属性,例如:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"lastName CONTAINS[cd] %@", searchText];