Core Data NSPredicate filter by entity class?

梦想与她 提交于 2020-01-03 09:12:11

问题


How would I create an NSPredicate to filter by entity of class Contact?

The solution to NSPredicate check for kind of object class crashes:

[NSPredicate predicateWithFormat:@"person.class == %@", [Contact class]];

*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
    reason: 'keypath person.class not found in entity <NSSQLEntity User id=12>'

Background

I have the Core Data entities User (app user) & Contact (address book contact), both children of Person (abstract entity). Person has many identities, which can be emails, phones, or usernames.

To create a text-message-like typeahead, I want to create an NSFetchRequest for Identity entities where person is of class Contact.


回答1:


You are perhaps mixing up a few things. The answer you indicate is about a predicate to filter an array of UIViews. A predicate in core data works in a slightly different way.

To achieve your desired result, simply set the entity of your fetch request to @"Contact".




回答2:


If you have a required field in Contact, you can filter on that field being not null. If you don't have a required field in Contact (that is not in Person), you could add a dummy required field and filter on that field.



来源:https://stackoverflow.com/questions/9984820/core-data-nspredicate-filter-by-entity-class

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