开发者

NSComparisonResult search 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 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?


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

for (Annotation *ano in listContent) {

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

    }


There is a straight forward way to do a substring check:

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 
}

NOTE: I am using this with a UISearchDisplayController and this worked perfectly for me.

Hope this helps you in future.

Thanks to this post (UPDATE: This link doesn't work anymore.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜