NSPredicate predicateWithFormat“OR”有问题
我正在尝试使用 NSPredicate 从 coredata 获取实体。我正在尝试采用“边输入边搜索”的方法。它适用于简单的产品名称,例如“椅子”或“桌子”。
但是,一旦我尝试了更复杂的东西,例如“轮椅 - 电动 - 电源”,那么当我输入“轮椅电动”时,结果就没有显示。我认为这与我的 NSPredicate predicateWithFormat 有关,但我已经这样做了一段时间了。所以我需要一些帮助。
这是我的代码(这些是我最好的猜测)
request.predicate = [NSPredicate predicateWithFormat:@" Name BEGINSWITH [c] %@ ",searchText];
request.predicate = [NSPredicate predicateWithFormat:@" Name BEGINSWITH [c] %@ OR Name like[cd] %@ ",searchText,searchText];
提前致谢:) 池塘
I am trying to fetch an entity from coredata using NSPredicate. I am trying to do a 'search as you type' approach. It works fine on a simple product name like "chair" or "Table".
But as soon as I tried the more complicate stuff like "Wheelchair - electric - power" then when I type in "Wheelchair electric" the result is not showing. I think it has to do with my NSPredicate predicateWithFormat but I've been at this for awhile now. So I need some help.
Here is my code (these are my best guesses)
request.predicate = [NSPredicate predicateWithFormat:@" Name BEGINSWITH [c] %@ ",searchText];
request.predicate = [NSPredicate predicateWithFormat:@" Name BEGINSWITH [c] %@ OR Name like[cd] %@ ",searchText,searchText];
Thanks in advance :)
Pondd
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要进行 OR 搜索,您必须首先将搜索词按空格分割到一个数组中。然后,您需要为每个单独的组件创建一个 LIKE 谓词。最后,您必须将各个谓词与多个 OR 复合谓词组合起来。
To have an OR search you will have to first split the search term by whitespace into an array. Then you need to create a LIKE predicate for each individual component. Finally you have to combine the individual predicates with several OR compound predicates.