Objective-C: NSPredicate 检查两个数组
Objective-C: NSPredicate Check Two Arrays
现在,我有一个可用的应用程序(类似于 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 上,用户可以 select 将用户添加到组中。现在,我有一个 'addedContacts' 数组。
当用户返回到 textField 以搜索更多用户以添加到组中时,我如何更改我的 NSPredicate 以包含组 (addedContacts) 中尚不存在的可用联系人(来自 searchableContacts)?
您可以将构建谓词的代码简化为:
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];
现在,我有一个可用的应用程序(类似于 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 上,用户可以 select 将用户添加到组中。现在,我有一个 'addedContacts' 数组。
当用户返回到 textField 以搜索更多用户以添加到组中时,我如何更改我的 NSPredicate 以包含组 (addedContacts) 中尚不存在的可用联系人(来自 searchableContacts)?
您可以将构建谓词的代码简化为:
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];