NSComparisonResult to search for part of a string?

I am using NSComparisonResult with my SearchController:

for (Annotation *ano in listContent) {
        NSComparisonResult result = [ano.title compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
        if (result == NSOrderedSame) {
            [self.filteredListContent addObject:ano];
        }
    }

If I search for a string, it will find the result only if it starts with this string.

Record " My art gallery "

  • Search "My art gallery" <--- Found

  • Find "My" <--- Found

  • Search for "Art" <--- Not found

  • Search "Gallery" <--- Not found

How can I change my code so that I can find parts of a string as I showed above?

+3
source share
2 answers

I ended up using NSRange, which allowed me to basically look for a substring:

for (Annotation *ano in listContent) {

        NSRange range = [ano.title rangeOfString:searchText options:NSCaseInsensitiveSearch];
        if (range.location != NSNotFound) {
            [self.filteredListContent addObject:ano];
        }

    }
+12
source

:

NSComparisonResult result1 = [dummyString compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:[dummyString rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)]];
if (result1 == NSOrderedSame)
{
 [self.filteredListContent addObject:dummyString];  // this can vary drastically 
}

. UISearchDisplayController, .

, .

(UPDATE: .)

+5

Source: https://habr.com/ru/post/1787572/


All Articles