有没有办法构造一个谓词来按类类型过滤?
我目前循环遍历数组并检查每个对象的类.也许有更干净的方式?
如何NSPredicate按类的实体创建过滤器Contact?
NSPredicate的解决方案检查对象类崩溃的类型:
[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>'
Run Code Online (Sandbox Code Playgroud)
我有核心数据实体User(app用户)和Contact(地址簿联系人),两个孩子Person(抽象实体).Person有很多identities,可以是emails,phones或usernames.
要创建类似文本消息的typeahead,我想创建一个NSFetchRequestfor 类的Identity实体.personContact
class core-data nspredicate nsfetchrequest nsentitydescription
我有两个班,公司和MyCompany.MyCompany是Company的子类,Company是NSManagedObject的子类.我正在尝试为NSFetchRequest编写谓词,该谓词将返回类Company的结果,但过滤掉MyCompany对象.
我尝试过以下内容(建议来自/sf/answers/564615481/):
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"class != %@",NSStringFromClass([myCompany class])];
Run Code Online (Sandbox Code Playgroud)
但是我收到一个错误:
'keypath class not found in entity <NSSQLEntity CKCompany id=1>'
Run Code Online (Sandbox Code Playgroud)
我也试过(建议从这里/sf/answers/818578841/,我知道我真的不想SELF isKindOfClass,但我只是用与答案中给出的完全相同的命令进行测试):
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self isKindOfClass: %@", [myCompany class];
Run Code Online (Sandbox Code Playgroud)
并得到以下错误:
'Unknown/unsupported comparison predicate operator type'
Run Code Online (Sandbox Code Playgroud)
如何编写谓词来实现我想要的?我支持iOS 5及更高版本.
nspredicate ×3
class ×1
cocoa ×1
cocoa-touch ×1
core-data ×1
ios ×1
ios5 ×1
nsarray ×1
objective-c ×1