gpt4 book ai didi

ios - NSPredicate SELF CONTAINS[c] 首先返回较长文本中包含的值

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:58:57 25 4
gpt4 key购买 nike

我正在使用 UISearchBar 对 UITableView 进行排序。表格中填充的数组中的每个值最多可以有 3 个单词

@“菠萝”、@“菠萝绿”、@“苹果绿”、@“生苹果绿”...

我正在使用下面的 NSPredicate 方法:

 NSArray *words = [text componentsSeparatedByString:@" "];
NSMutableArray *predicateList = [NSMutableArray array];
for (NSString *word in words) {
if ([word length] > 0) {
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", word];
[predicateList addObject:pred];
}
}
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicateList];
NSLog(@"%@", predicate);

它有效,但它不会按最接近的亲戚对值进行排序,而是始终按其索引从数组中获取第一个匹配值。这意味着,如果我想查找“apple”并在 SearchBar 中键入它,结果将是:

Pineapple
Pineapple Green
Apple
Apple Green

我不能使用谓词 BEGINSWITH,因为如果我搜索“Green Apple”,结果将是空白。在这种特殊情况下,我可以使用升序对结果进行排序,但在另一种情况下(如 TEA),它不起作用,因为字符串“TEA”包含在其他单词(如 STEAM)中,它会排在第一位。

我在这里没有选择,有没有办法根据搜索输出逻辑结果?如果要查找“Apple”:

Apple    
Apple Green

//-------- 添加

谢谢大家的回复。我在考虑自定义逻辑并想出了这个。我确信它是脏代码并且可以改进,但它现在的性能要好得多。它仍然没有以有组织的方式显示结果,但是搜索现在包含所有相关的项目,无论您以什么顺序输入搜索:

NSArray *words = [text componentsSeparatedByString:@" "];

NSString *word1;
NSString *word2;
NSString *word3;
NSPredicate *predicate;

if (words.count == 1) {
word1 = [NSString stringWithFormat:@"%@", [words objectAtIndex:0]];
predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@ OR SELF LIKE[cd] %@", word1, word1];
}
else if (words.count == 2) {
word1 = [NSString stringWithFormat:@"%@", [words objectAtIndex:0]];
word2 = [NSString stringWithFormat:@"%@", [words objectAtIndex:1]];
predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@ AND SELF CONTAINS[cd] %@", word1, word2];
}
else if (words.count == 3) {
word1 = [NSString stringWithFormat:@"%@", [words objectAtIndex:0]];
word2 = [NSString stringWithFormat:@"%@", [words objectAtIndex:1]];
word3 = [NSString stringWithFormat:@"%@", [words objectAtIndex:2]];
predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@ AND SELF CONTAINS[cd] %@ AND SELF CONTAINS[cd] %@", word1, word2,word3];
}
NSArray *sortedArray = [self.allItemsArray filteredArrayUsingPredicate:predicate];
filteredTableData = [sortedArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

如果有人知道如何对结果进行排序以获得最接近的匹配,请分享。谢谢

最佳答案

解决这个问题的一种方法是将谓词应用于 NSSet 而不是 NSArray。像这样:

     NSSet *tempSet = [NSSet setWithArray:words];
tempSet = [tempSet filteredSetUsingPredicate:predicate];
resultsArray = [tempSet allObjects];

希望这对您有所帮助。

关于ios - NSPredicate SELF CONTAINS[c] 首先返回较长文本中包含的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34383044/

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