How to filter array with NSPredicate for combination of words

空扰寡人 提交于 2019-12-05 18:01:35
Mundi
[NSPredicate predicateWithFormat:@"self CONTAINS[cd] %@", searchText];

EDIT after expansion of question

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;

This will have the results in the desired order without duplicate entries.

EDIT Actually beginsWith check from start of string to search string length. if exact match found then its filtered

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

contains also check from start of string to search string length but if found start, middle or end exact search string then it is filtered.

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];
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!