NSComparisonResult 搜索字符串的一部分?

发布于 2024-10-14 01:28:40 字数 663 浏览 5 评论 0原文

我将 NSComparisonResult 与我的 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];
        }
    }

如果我搜索一个字符串,它只会找到以该字符串开头的结果。

记录是“我的艺术画廊

  • 搜索“我的艺术画廊”<--- 找到

  • 搜索“我的”<--- 找到

  • 搜索“艺术”<--- 未找到

  • 搜索“画廊”<---未找到

如何更改我的代码以便我可以找到上面所示的字符串的一部分吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挖个坑埋了你 2024-10-21 01:28:40

我最终使用了 NSRange,它允许我基本上搜索子字符串:

for (Annotation *ano in listContent) {

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

    }

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

    }

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

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 一起使用,这对我来说非常有效。

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

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

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文