3

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 only find the result if it starts with that string.

Record is "My Art Gallery"

  • Search for "My Art Gallery" <--- Found

  • Search for "My " <--- Found

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

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

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

4

2 回答 2

12

我最终使用了 NSRange,它让我基本上可以搜索一个子字符串:

for (Annotation *ano in listContent) {

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

    }
于 2011-01-24T23:22:21.787 回答
6

有一种直接的方法来进行子字符串检查:

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 
}

注意:我将它与 aUISearchDisplayController一起使用,这对我来说非常有效。

希望这对您将来有所帮助。

感谢这篇文章(更新:此链接不再有效。)

于 2012-03-20T15:06:47.887 回答