问题
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