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

时间:2015-12-20 16:18:27

标签: ios objective-c uitableview sorting nspredicate

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

@" Pineapple",@" Pineapple Green",@" Apple Green",@" Apple Green,Raw" ...

我在下面使用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",结果将为空白。在这种特殊情况下,我可以用来对结果Ascending进行排序,但在另一种情况下,例如TEA,它不会起作用,因为字符串" TEA"其中包含STEAM等其他词语。

我没有选择,有没有办法根据搜索输出逻辑结果?在寻找" Apple":

的情况下
Apple    
Apple Green

// ------- ADITION

感谢您的回复。我在考虑自定义逻辑并提出了这个问题。我确信它是脏代码并且可以改进,但它现在表现得更好。它仍然没有以有条理的方式显示结果,但搜索包含所有相关的项目,无论您键入搜索的顺序如何:

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:)];

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

1 个答案:

答案 0 :(得分:1)

轰!我知道了!我必须添加的是NSSortDescriptor。现在看起来很简单:

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];
    NSSortDescriptor *lengthSorter = [NSSortDescriptor sortDescriptorWithKey:@"length" ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObject:lengthSorter];
    filteredTableData = [sortedArray sortedArrayUsingDescriptors:sortDescriptors];

它按照我现在想要的方式工作!但如果您有任何改进建议,请分享!

谢谢