NSComparisonResult 搜索字符串的一部分?
我将 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" <---
FoundSearch 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最终使用了 NSRange,它允许我基本上搜索子字符串:
I ended up using NSRange which allowed me to basically search for a substring:
有一种直接的方法可以进行子字符串检查:
注意:我将其与 UISearchDisplayController 一起使用,这对我来说非常有效。
希望这对您将来有所帮助。
感谢这篇文章(更新:此链接不再有效。)
There is a straight forward way to do a substring check:
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.)