Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在NSPredicates中使用密钥路径_Objective C_Key Value Observing_Key Value Coding - Fatal编程技术网

Objective c 在NSPredicates中使用密钥路径

Objective c 在NSPredicates中使用密钥路径,objective-c,key-value-observing,key-value-coding,Objective C,Key Value Observing,Key Value Coding,我有一个包含(我的自定义)GTPerson对象的NSDictionary。GTPerson有一个NSMutableSet*parents属性,我在该属性上使用@property和@synthesis 在我的NSDictionary中,我想筛选所有没有父对象的GTPerson对象,即父对象计数为0的对象 我正在使用以下代码: NSPredicate *predicate = [NSPredicate predicateWithFormat:@"parents.count = 0"

我有一个包含(我的自定义)GTPerson对象的NSDictionary。GTPerson有一个
NSMutableSet*parents
属性,我在该属性上使用
@property
@synthesis

在我的NSDictionary中,我想筛选所有没有父对象的GTPerson对象,即父对象计数为0的对象

我正在使用以下代码:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"parents.count = 0"];
NSArray *np = [[people allValues] filteredArrayUsingPredicate:predicate];
执行此操作时,我收到以下错误:

[valueForUndefinedKey:]:此类不符合密钥计数的键值编码。

由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[valueForUndefinedKey:]:此类不符合密钥计数的键值编码要求。”


为什么它试图在GTPerson上调用
count
,而不是在其
parents
属性上调用?

解决问题的方法是使用操作符
@count
,就像在
@“parents.@count==0”
中一样

读取异常时,我们看到评估谓词将消息
-count
发送到GTPerson对象。为什么?

-valueForKey:
发送到集合(在您的情况下,该集合是对键路径的
父组件
求值的结果)将
-valueForKey:
发送到集合中的每个对象

在这种情况下,会将
-valueForKey:@“count”
发送到每个GTPerson实例,并且GTPerson不符合count的键值编码

相反,当需要集合的计数时,使用
@count
操作符计算集合的计数,而不是集合中所有对象的键
count
的值