目前,我有一个有效的应用程序(类似于WhatsApp),用户可以使用NSTextField进行实时搜索,将用户添加到群聊中。使用NSPredicate,我搜索一个数组(searchableContacts)来查找应用程序上的任何可用联系人,并将匹配输出到UITableView,如下所示:
- (void)textFieldDidChange :(UITextField *)theTextField {
[filteredArray removeAllObjects];
NSMutableArray *partPredicates = [NSMutableArray arrayWithCapacity:2];
NSPredicate *currentPartPredicate1 = [NSPredicate predicateWithFormat:@"firstName CONTAINS[cd] %@", self.searchField.text];
NSPredicate *currentPartPredicate2 = [NSPredicate predicateWithFormat:@"lastName CONTAINS[cd] %@", self.searchField.text];
[partPredicates addObject:currentPartPredicate1];
[partPredicates addObject:currentPartPredicate2];
NSPredicate *fullPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:partPredicates];
filteredArray = [[self.searchableContacts filteredArrayUsingPredicate:fullPredicate] mutableCopy];
[self.searchableMembers reloadData];
}
在生成的tableView上,用户可以选择要添加到组的用户。现在,我有一个'addedContacts'数组。
当用户返回textField以搜索要添加到组中的更多用户时,如何更改我的NSPredicate以包含组中尚未存在的可用联系人(来自searchableContacts)(addedContacts)?
答案 0 :(得分:0)
您可以简化用于构建谓词的代码:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstName CONTAINS[cd] %@ OR lastName CONTAINS[cd] %@", self.searchField.text, self.searchField.text];
您可以使用此选项调整它以排除addedContacts
中的项目:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(NOT SELF IN %@) AND (firstName CONTAINS[cd] %@ OR lastName CONTAINS[cd] %@)", self.addedContacts, self.searchField.text, self.searchField.text];