NSPredicate that filters out subclass results

柔情痞子 提交于 2019-12-10 04:46:54

问题


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.

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

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

But I get an error:

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

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];

And got the following error:

'Unknown/unsupported comparison predicate operator type'

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


回答1:


You can set

[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.



来源:https://stackoverflow.com/questions/16636338/nspredicate-that-filters-out-subclass-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!