gpt4 book ai didi

ios - 使用 NSPredicate 搜索 - 全名

转载 作者:行者123 更新时间:2023-11-29 11:45:53 27 4
gpt4 key购买 nike

已经将近 3 天了,我仍然无法修复该错误。简单的情况,我想搜索人名(姓名和姓氏)。

示例:

  1. 首先我写给 John(结果:John Newman、John Stable、John Carens)
  2. 然后我用空格写 John(结果:无)
  3. 之后我继续与 John N(结果:John Newman)合作。我需要保持即使我在搜索中输入空格,也会显示名称。

这只是 if/else 的第一部分,我在其中处理 2 个以上单词的数组。谢谢


- (void)searchForText:(NSString*)searchText
{
NSString *searchTextt = [searchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

NSArray *array = [searchTextt componentsSeparatedByString:@" "];


NSString *firstName = searchTextt;
NSString *lastName = searchTextt;
NSString *firstName2 = searchTextt;
NSString *lastName2 = searchTextt;

NSPredicate *predicate = nil;

if ([array count] > 1) {
firstName = array[0];
lastName = array[1];
firstName2 = array[1];
lastName2 = array[0];

predicate = [NSPredicate predicateWithFormat:@"(guestName CONTAINS[cd] %@ AND guestSurname CONTAINS[cd] %@)", firstName, lastName];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"(guestSurname CONTAINS[cd] %@ AND guestName CONTAINS[cd] %@)", lastName2, firstName2];

NSPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[predicate, predicate2]];


self.searchResults = [self.mainarray filteredArrayUsingPredicate:compoundPredicate];

} else {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"guestSurname contains[cd] %@", searchText];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"guestName contains[cd] %@", searchText];
NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@"guestCompany contains[cd] %@", searchText];


NSPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[predicate, predicate2, predicate3]];
self.searchResults = [self.mainarray filteredArrayUsingPredicate:compoundPredicate];

}


}

编辑:

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSString *searchString = searchController.searchBar.text;
[self searchForText:searchString];
[_maintable reloadData];
}

最佳答案

我有一个类似的数组

( { guestName = John; guestSurname = Newman; }, { guestName = John; guestSurname = Stable; }, { guestName = John; guestSurname = Carens; } )

我写函数

- (void)searchForText:(NSString*)searchText 
{
searchTextt = [searchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSArray *array = [searchTextt componentsSeparatedByString:@" "];
NSString *firstName = searchTextt;
NSString *lastName = searchTextt;
NSPredicate *predicate = nil;

if ([array count] > 1) {
firstName = array[0];
lastName = array[1];
predicate = [NSPredicate predicateWithFormat:@"(guestName CONTAINS[cd] %@ AND guestSurname CONTAINS[cd] %@) OR (guestName CONTAINS[cd] %@ AND guestSurname CONTAINS[cd] %@)", firstName, lastName, lastName, firstName];

} else {
predicate = [NSPredicate predicateWithFormat:@"guestName CONTAINS[cd] %@ OR guestSurnamen CONTAINS[cd] %@", firstName, lastName];

}
NSArray *arrResult = [arr filteredArrayUsingPredicate:predicate];;

}

- (void)updateSearchResultsForSearchController:(UISearchController
*)searchController
{
NSString *searchString = searchController.searchBar.text;
[self searchForText:searchString];
[_maintable reloadData];
}

试试这个。这对我有用,当我在文本字段中输入“john”时,我得到了结果。

关于ios - 使用 NSPredicate 搜索 - 全名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43904439/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com