我有以下uisearchbar委托方法。我希望此方法搜索我的对象arrayofterms属性,以查看是否有任何元素与searchText参数匹配。
这会有用吗? :
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
[self.filteredArray removeAllObjects];
// something like this is what the objects array attribute will look like: NSArray *arrayofterms = [NSArray arrayWithObjects:@"Miguel", @"Ben", @"Adam", @"Melissa", nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.arrayofterms contains[c] %@",searchText];
self.filteredArray = [NSMutableArray arrayWithArray:[self.planDataArray filteredArrayUsingPredicate:predicate]];
}
答案 0 :(得分:0)
数组是否包含字符串?然后......
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(NSString *string, NSDictionary *bind) {
return [string isEqualToString:searchText];
}];
self.filteredArray = [[self.planDataArray filteredArrayUsingPredicate:predicate] mutableCopy];
如果planDataArray包含其他类型的对象,可能是自定义对象,那么......
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(MyCustomObject *object, NSDictionary *bind) {
NSString *string = object.someStringProperty;
return [string isEqualToString:searchText];
}];