2015-08-31 30 views
0

現在,我有一個工作應用程序(類似於WhatsApp),用戶可以使用NSTextField進行實時搜索以將用戶添加到羣聊中。使用NSPredicate,我通過一個數組(searchableContacts)找上了應用程序的任何可用的聯繫人搜索,像火柴輸出到一個UITableView,像這樣:Objective-C:NSPredicate Check Two Arrays

- (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

可以簡化你的代碼爲建設謂語這樣:

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]; 
+0

完美!謝謝 :) –