NSPredicate过滤掉子类结果 [英] NSPredicate that filters out subclass results

查看:156
本文介绍了NSPredicate过滤掉子类结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个班,公司和MyCompany。 MyCompany是Company的子类,Company是NSManagedObject的子类。我正在尝试为NSFetchRequest编写谓词,该谓词将返回类Company的结果,但会过滤掉MyCompany对象。

I have two classes, Company and MyCompany. MyCompany is a subclass of Company, and Company is a subclass of NSManagedObject. I am trying to write a predicate for an NSFetchRequest that will return results of the class Company, but filter out MyCompany objects.

我尝试了以下(从这里建议< a href =https://stackoverflow.com/a/8065935/472344> https://stackoverflow.com/a/8065935/472344 ):

I have tried the following (suggested from here https://stackoverflow.com/a/8065935/472344):

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"class != %@",NSStringFromClass([myCompany class])];

但我收到错误:

'keypath class not found in entity <NSSQLEntity CKCompany id=1>'

我也试过了(建议从这里 https://stackoverflow.com/a/11693983/472344 ,我知道我真的不想SELF isKindOfClass,但我只是用与答案中给出的完全相同的命令进行测试):

I also tried (suggested from here https://stackoverflow.com/a/11693983/472344, I know I really want not SELF isKindOfClass, but I was just testing with the exact same command as given in the answer):

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self isKindOfClass: %@", [myCompany class];

并收到以下错误:

'Unknown/unsupported comparison predicate operator type'

我该怎么办?写一个谓词来实现我想要的东西?我支持iOS 5及以上版本。

How can I write a predicate to achieve what I want? I am supporting iOS 5 and above.

推荐答案

你可以设置

[fetchRequest setIncludesSubentities:NO];

以便获取请求仅返回请求的实体类型的对象,

so that the fetch request returns only objects of exactly the entity type of the request, and does not include subentities.

如果谓语用于核心数据获取请求。

It is (as far as I know) not possible to refer to the objects class or entity name in a predicate if the predicate is used in a Core Data fetch request.

这篇关于NSPredicate过滤掉子类结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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