I am trying to do a filter by comparing a value (‘stringToCompare’) I have in a list and the search text entered by the user (‘searchText’). The code snippet as below.
NSComparisonResult result = [stringToCompare compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
NSLog(@"Search text: %@", searchText);
NSLog(@"string to compare: %@", stringToCompare);
NSLog(@"result: %i", result);
NSLog(@"NSOrderedsame: %i", NSOrderedSame);
if (result == NSOrderedSame)
{
[self.filteredListContent addObject:genericObj];
}
However, as can be seen in the NSLog results below, I can’t seem to be able to get the expected result. In this case, I am expecting the result to be ‘0’, since the search text ‘Xot’ can be found as part of ‘stringToCompare’. What am I missing here?
2012-01-02 05:13:59.184 onethingaday[5815:707] search text: Xot
2012-01-02 05:13:59.185 onethingaday[5815:707] string to compare: One exotic place you got laid in before. How was it?
2012-01-02 05:13:59.188 onethingaday[5815:707] result: -1
2012-01-02 05:13:59.188 onethingaday[5815:707] NSOrderedsame: 0
You are comparing entire strings, not looking for occurences. Try this: