ios 使用多个条件配置 NSPredicate

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6004620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:55:41  来源:igfitidea点击:

Configuring an NSPredicate with multiple conditions

iphoneobjective-cxcodeioscore-data

提问by Will

I'm not certain how one would go about "cascading" several conditions into an NSPredicate.

我不确定如何将几个条件“级联”到 NSPredicate 中。

I'm fairly new to Core Data and not sure if that's the right way to achieve what I am looking to do.

我对 Core Data 还很陌生,不确定这是否是实现我想要做的事情的正确方法。

Here is what I am trying to do:

这是我想要做的:

There is a Photo object that has a whereTook relationship to a Location object, which has an inverse of photosTookThere.

有一个 Photo 对象与一个 Location 对象具有 whereTook 关系,Location 对象与 photosTookThere 是相反的。

The Photo in turn has an NSNumber attribute called isFavourite.

照片又具有一个名为 isFavourite 的 NSNumber 属性。

I'd like to configure an NSFetchRequest that will first check that photosTookThere exists and if so, check each resulting Photo object and return only the ones that are set as favourites.

我想配置一个 NSFetchRequest,它将首先检查 photosTookThere 是否存在,如果存在,则检查每个结果 Photo 对象并仅返回设置为收藏夹的对象。

Here is the code so far:

这是到目前为止的代码:

request.entity = [NSEntityDescription entityForName:@"Location" inManagedObjectContext:context];
request.sortDescriptors = [NSArray arrayWithObject[NSSortDescriptorsortDescriptorWithKey:@"locationId" ascending:YES]];
request.predicate = [NSPredicate predicateWithFormat:@"photosTookThere.@count != 0"];

How would I cascade the second condition into the predicate and return the correct results?

我如何将第二个条件级联到谓词中并返回正确的结果?

回答by Mark Leonard

Just put each condition inside of parentheses and connect them with AND.

只需将每个条件放在括号内并用 AND 连接它们。

[NSPredicate predicateWithFormat:@"(photosTookThere.@count != 0) AND (isFavourite == %@)", [NSNumber numberWithBool:YES]];

I'd also recommend changing the name of your "whereTook" relationship to "location", and your "photosTookThere" to simply "photos", that's more in line with convention.

我还建议将您的“whereTook”关系的名称更改为“location”,将您的“photosTookThere”更改为简单的“photos”,这更符合惯例。

回答by Will

I eventually was able to solve this using a subquery:

我最终能够使用子查询解决这个问题:

request.predicate = [NSPredicate predicateWithFormat:
                         @"(photosTookThere.@count !=0) AND SUBQUERY(photosTookThere, $Photo, $Photo.isFavourite==1).@count >0"
                         ];

回答by pzearfoss

You can very simply use AND and OR in your predicate much like you would if you were specifying a "where" condition in an SQL statement. To check for a NULL, which it looks like you want when comparing against "whereTook", you can compare to nil (whereTook = nil).

您可以非常简单地在谓词中使用 AND 和 OR,就像在 SQL 语句中指定“where”条件一样。要检查在与“whereTook”进行比较时看起来像是您想要的 NULL,您可以与 nil (whereTook = nil) 进行比较。