核心数据,NSPredicate,ANY key.path == nil [英] Core Data, NSPredicate, ANY key.path == nil

查看:312
本文介绍了核心数据,NSPredicate,ANY key.path == nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想出了一个解决方案使用子查询,但我不明白为什么我第一次尝试做不起作用。

I came up with a solution to this using subquery, but I don't understand why what I was trying to do first didn't work.

这里是我的数据模型。

Here's my data model. I'm fetching on Advice.

我可以作为谓词执行以下操作:

I can do the following as a predicate:

[NSPredicate predicateWithFormat:@"ANY conditions.terrain == %@", aTerrainObject];

工作正常,并返回任何一条建议,其中至少一个条件具有地形类型。

that works fine, and returns any piece of advice where at least one of its conditions has that terrain type.

但是,当我尝试这样做时,它会失败:

However, when I try to do this, it fails:

[NSPredicate predicateWithFormat:@"ANY conditions.terrain == nil"];

我想要做的是返回任何一条建议,其中至少一个条件不

What I want to do is return any piece of advice where at least one of its conditions doesn't have a terrain type set.

但是,以下操作有效:

[NSPredicate predicateWithFormat:@"SUBQUERY(conditions, $x, $x.terrain == nil).@count > 0"];

任何人都可以解释为什么在搜索nil时,我不能使用 ANY 语法?

Can anyone explain why, when searching for nil, I can't use the ANY syntax?

推荐答案


对于nil,我不能使用ANY语法?

Can anyone explain why, when searching for nil, I can't use the ANY syntax?

是的!这是发生了什么。

Yep! Here's what's going on.

[NSPredicate predicateWithFormat:@"ANY conditions.terrain == nil"];

首先,我们将它分成适当的左右表达式:

First, let's break this up into the appropriate left and right expressions:

conditions.terrain

将通过采用 SELF 对象(建议实例)并请求 valueForKeyPath: @conditions.terrain。此关键字的结果将是一个集合。您实际上是:

This will be evaluated by taking the SELF object (an Advice instance) and requesting the valueForKeyPath:@"conditions.terrain". The result of this keypath will be a collection. You're essentially doing:

Advice *a = ...;
NSSet *conditions = [a conditions];
NSSet *terrains = [conditions valueForKey:@"terrain"];

所以,你有一个潜在的集合 Terrain 实例。现在,我们对Objective-C中的集合有什么了解?首先,它们不能包含 nil 。它们只能包含对象。这意味着当它执行谓词的 ANY 部分时,它将遍历数组中的项目,并且看到它们都不是 nil

So, you have a collection of (potential) Terrain instances. Now, what do we know about collections in Objective-C? Well for one thing, they cannot contain nil. They can only contain objects. This means that when it executes the ANY portion of the predicate, it's going to iterate through the items in the array and see that none of them are nil.

因此,您的谓词失败。我试着玩一些其他变化(使用 [NSNull null] 而不是 nil 等)他们似乎工作。

Thus, your predicate is failing. I tried playing around with some other variations (using [NSNull null] instead of nil, etc), but none of them seemed to work.

因此,你会使用 SUBQUERY 来解决这个问题你可以得到。我强烈建议您提交错误,详述您的期望,并说明您认为此操作应该如何运作的原因。

It would therefore appear that your use of a SUBQUERY to solve this is about as good as you could get. I would highly recommend filing a bug detailing your expectations and expressing why you think this should work.

这篇关于核心数据,NSPredicate,ANY key.path == nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆