使用 NSPredicate 获取 Core Data 对象数组?
假设我有一个名为 Person 的核心数据实体。我如何获得属性与某些值匹配的人员的 NSArray?例如,具有特定年龄、身高或体重的人...或者具有特定值的身高、体重和年龄的人...
我可以像这样使用 NSPredicate 吗:
NSPredicate *pred =
[NSPredicate predicateWithFormat:
@"(age == 25) OR (height_in_cms == 185) OR (age == 30 AND height_in_cms == 170 AND weight_in_kgs == 80)";
// All properties are NSNumber
Say I have a Core Data entity called Person. How would I get an NSArray of Persons whose properties match certain values? For instance someone of a particular age, height, or weight... or someone with a whose height,weight and age are specific values...
Can I use an NSPredicate like so:
NSPredicate *pred =
[NSPredicate predicateWithFormat:
@"(age == 25) OR (height_in_cms == 185) OR (age == 30 AND height_in_cms == 170 AND weight_in_kgs == 80)";
// All properties are NSNumber
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(2)
我不是
predicateWithFormat:
语法方面的专家,但您已经了解了基本要点。您可以在 Apple 的 Predicate 中找到有关格式的详细信息编程指南。如果您询问拥有谓词后如何处理该谓词,这里有一个片段向您展示了步骤:如果您希望对结果进行排序,您可以使用
setSortDescriptors:
在执行获取之前。I'm not an expert on the syntax for
predicateWithFormat:
, but you have the basic gist. You can find details on the format in Apple's Predicate Programming Guide. If you're asking what to do with the predicate once you have it, here is a snippet that shows you the steps:If you want the results to be sorted, you can pass an array of sort descriptors to the fetch request using
setSortDescriptors:
prior to executing the fetch.如果变量中有这些值,则可以遵循给定的语句。
[fetchResults filterUsingPredicate:[NSPredicate predicateWithFormat:@"age == %i OR hieght== %i AND Weight==%i",age,height,weight]];
而且,对于特定值,您的方法是正确的,但您的语句有语法错误,因此请保持正确的语法
You can follow the given statement if you have these value in a variable.
[fetchResults filterUsingPredicate:[NSPredicate predicateWithFormat:@"age == %i OR hieght== %i AND weight==%i",age,height,weight]];
And also your approach is correct in case for specific values but your statement having syntax error so maintain proper syntax