0

I have a searchBar and a searchController in my project. I want to be able to search for a specific sequence of letters and possibly skipping a character or two if possible. For example if I search for "iPhone" or "i-phone" or "i phone" or "piphone" or "phone" i want to be able to still find the search term "iphone" in the list. Any help would be appreciated.

Where searchTerm is an all uppercase string from UISearchBar

NSCharacterSet *allowedChars = [[NSCharacterSet characterSetWithCharactersInString:@" ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890"] invertedSet];
NSString *resultString = [[searchTerm componentsSeparatedByCharactersInSet:allowedChars] componentsJoinedByString:@" "];

Taking in mind that the @" " in the allowsChars is essential because the actual search could be @"iPhone 32gb" etc...

snksnk
  • 1,566
  • 4
  • 21
  • 46
  • I don't know how to continue. The only thing I did try was to convert the final string to all capitals and remove any special characters. I will edit the post with the code – snksnk Feb 18 '14 at 11:51
  • You tried using `NSPredicate`? Your data is strings in an array, or dictionaries in an array? – Wain Feb 18 '14 at 11:55
  • The data will is downloaded from a cloud database – snksnk Feb 18 '14 at 11:56
  • the data will be fetched using a single query call – snksnk Feb 18 '14 at 12:05

1 Answers1

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

It will log as

2014-02-18 06:09:52.911 demo[5261] Bobs: ("i-phone", "i phone", phone, "apple phone")
Muruganandham K
  • 5,271
  • 5
  • 34
  • 62