使用 NSPredicate 获取 Core Data 对象数组?

发布于 2024-10-03 12:53:53 字数 349 浏览 7 评论 0原文

假设我有一个名为 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 技术交流群。

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

发布评论

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

评论(2

時窥 2024-10-10 12:53:53

我不是 predicateWithFormat: 语法方面的专家,但您已经了解了基本要点。您可以在 Apple 的 Predicate 中找到有关格式的详细信息编程指南。如果您询问拥有谓词后如何处理该谓词,这里有一个片段向您展示了步骤:

// Create a fetch request.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

// Set the entity for the fetch request.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"EntityName" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
[entity release];

// Set the predicate for the fetch request.
[fetchRequest setPredicate:predicate];

// Perform the fetch.
NSError *error;
NSArray *array = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];

如果您希望对结果进行排序,您可以使用 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:

// Create a fetch request.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

// Set the entity for the fetch request.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"EntityName" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
[entity release];

// Set the predicate for the fetch request.
[fetchRequest setPredicate:predicate];

// Perform the fetch.
NSError *error;
NSArray *array = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];

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.

幼儿园老大 2024-10-10 12:53:53

如果变量中有这些值,则可以遵循给定的语句。

[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

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