CoreData BEGINSWITH 一对多关系
CoreData 到目前为止真的很棒:-) 我真的很喜欢它。 现在我已经有了一对多关系并想要对其进行过滤。我被困在这里了。例如,我有多个邮政编码(44512、12445,...) 问题是,如果我搜索 445
并使用 contains = %@
它会返回两个结果 (12345, 12445)。
我想得到一个结果,该结果应该以我的搜索字符串开头。所以通常我会使用 BEGINSWITH = %@
。对于多对多关系,我无法使用它。有什么解决方法吗? :-)
非常感谢您的帮助,我真的很感激!
CoreData so far is really great :-) I really get into it.
Now I've got a to-many relationship and want to filter that. I'am stuck here. I have multiple zipcodes for example (44512, 12445, ...)
The problem is, if I search for 445
and I use contains = %@
it returns two results (12345, 12445).
I want to get exactly one result, the result should begin with my searchstring. So normally I would use BEGINSWITH = %@
. With a to-many relationship I can't use that. Is there any workaround? :-)
Thanks a lot for you help, I really appreciate it!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有看到您的代码,我无法完全给出答案。鉴于您的其他问题,您的 Zips 似乎存储为 NSStrings。
因此,您可以构造一个 NSPredicate 并使用
beginswith
,如 来自 Apple 的示例。beginswith
示例位于数组部分,但您可以轻松更改示例的谓词并将其应用到 NSFetchRequest。如果您以前从未执行过谓词,则 NSFetchRequest 有一个predicate
属性。您创建 predate,然后将其分配给 NSFetchRequest 的 predicate 属性,然后执行您的获取请求。就像我在开始时所说的那样,如果您显示代码片段,我们的答案就会更有帮助/更具体。
Without seeing your code I can't completely give an answer. Given your other SO question it looks like your Zips are stored as NSStrings.
So, you can construct an NSPredicate and use
beginswith
like in this example from Apple. Thebeginswith
example is in the Arrays section but you can easily change the predicate for your example and apply it to your NSFetchRequest. If you've never done a predicate before, the NSFetchRequest has apredicate
property. You create the predate, then assign it to the NSFetchRequest's predicate property and then execute your fetch request.Like I said at the beginning, if you show a code snippet we can be more helpful/specific in our answers.