我的项目中有searchBar
和searchController
。我希望能够搜索特定的字母序列,并且如果可能的话可能会跳过一两个字符。例如,如果我搜索“iPhone”或“i-phone”或“i phone”或“piphone”或“phone”,我希望仍能在列表中找到搜索词“iphone”。任何帮助,将不胜感激。
其中searchTerm是来自UISearchBar的全部大写字符串
NSCharacterSet *allowedChars = [[NSCharacterSet characterSetWithCharactersInString:@" ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890"] invertedSet];
NSString *resultString = [[searchTerm componentsSeparatedByCharactersInSet:allowedChars] componentsJoinedByString:@" "];
请记住,allowChars中的@“”是必不可少的,因为实际的搜索可能是@“iPhone 32gb”等......
答案 0 :(得分:1)
NSArray *firstNames = [[NSArray alloc] initWithObjects: @"i-phone", @"i phone", @"phone", @"Quentin",@"apple phone",@"phoapple" ];
NSString * searchStr = @"phone";
NSPredicate *thirtiesPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@",searchStr];
NSLog(@"Bobs: %@", [firstNames filteredArrayUsingPredicate:thirtiesPredicate]);
它将记录为
2014-02-18 06:09:52.911 demo[5261] Bobs: ("i-phone", "i phone", phone, "apple phone")