【发布时间】:2017-05-19 14:49:13
【问题描述】:
我在代码数据中有一个名为“位置”的实体,而“位置”实体有大约 50 个属性。
我使用以下谓词来检查给定值是否与“位置”实体中的任何属性匹配(我不想将其与任何特定属性匹配,而是与实体中的所有属性匹配)
NSString *enumKey=@"Pune";
NSArray *enumListForDeleteArray= [[CoreDataModel sharedCoreDataModel] arrayOfRecordsForEntity:@"Location" andPredicate:[NSPredicate predicateWithFormat:@"%@",enumKey] andSortDescriptor:nil forContext:nil];
问题:确保值“Pune”与“Location”中的任何属性匹配的谓词应该是什么?
请建议 NSPredicate 值。
EIDT
NSString *enumKey = @"abc";
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Location" inManagedObjectContext:[[CoreDataModel sharedCoreDataModel] getNewManagedObjectContext]];//
NSMutableArray *orPredicateArray=[[NSMutableArray alloc] init];
for(NSDictionary *attributeName in entity.attributesByName) {
[orPredicateArray addObject:[NSPredicate predicateWithFormat:@"%K == %@",attributeName,enumKey]];
}
NSCompoundPredicate *combinedPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:orPredicateArray];
NSArray *enumKeyReferenceArray= [[CoreDataModel sharedCoreDataModel] arrayOfRecordsForEntity:@"Location" andPredicate:combinedPredicate andSortDescriptor:nil forContext:nil];
现在任何属性中都有“abc”值,但我仍然得到“位置”的对象?
【问题讨论】:
-
@Larme。感谢您的回复,但我没有找到与任何实体的所有属性进行比较的单个谓词。
标签: ios objective-c core-data