4
[filteredArray filterUsingPredicate:
   [NSPredicate predicateWithFormat:@"self BEGINSWITH[cd] %@", searchText]];

filteredArray包含简单的 NSStrings。[你好,我的,起来,起​​来,七等……];

它将给出所有以 . 开头的字符串searchText

但如果字符串是“我的名字是”和searchText = name. NSPredicate实现这一目标会是什么样子?

更新:如果我想得到结果searchText = name,但不是,那该searchText = ame怎么办?也许是这样的:

 [filteredArray filterUsingPredicate:
       [NSPredicate predicateWithFormat:
        @"self BEGINSWITH[cd] %@ or self CONTENTS[cd] %@", 
        searchText, searchText]];

但它应该首先显示以searchText包含searchText.

4

2 回答 2

5
[NSPredicate predicateWithFormat:@"self CONTAINS[cd] %@", searchText];

扩展问题后编辑

NSArray *beginMatch = [filteredArray filteredArrayUsingPredicate:
  [NSPredicate predicateWithFormat:
    @"self BEGINSWITH[cd] %@", searchText]];
NSArray *anyMatch = [filteredArray filteredArrayUsingPredicate:
  [NSPredicate predicateWithFormat:
    @"self CONTAINS[cd] %@", searchText]];

NSMutableArray *allResults = [NSMutableArray arrayWithArray:beginMatch];
for (id obj in anyMatch) {
   if (![allResults containsObject:obj]) {
      [allResults addObject:obj];
   }
}
filteredArray = allResults;

这将按所需顺序获得结果,而不会出现重复条目​​。

于 2012-09-20T10:16:39.533 回答
2

编辑 实际上开始检查从字符串开始到搜索字符串长度。如果找到完全匹配,则对其进行过滤

if u have name game tame lame
search Text : ame
filtered text would be: none

还包含从字符串开头到搜索字符串长度的检查,但如果找到开头、中间或结尾的精确搜索字符串,则将其过滤。

if u have name game tame lame
search Text : ame
filtered text would be: name game tame lame because all has ame

[NSPredicate predicateWithFormat:@"self CONTAINS '%@'", searchText];
于 2012-09-20T10:20:25.177 回答