问题描述
我有一个核心数据模型如下
I have an core data model as follows
的属性属性是一组 DictionaryEntry ,它们是我的Page对象的值,很像一个标准 NSDictionary (除了所有键和值是字符串)
The attributes property of Page is a set of DictionaryEntry, they are values for my Page objects, much like a standard NSDictionary (except all of the keys and values are strings)
与键=title具有 DictionaryEntry 和 value =Home。
推荐答案
您应该查看子查询的谓词语法。您不能使用通常的ANY关键字,因为这只允许您匹配一列而不是两个同时。
You should have a look at the predicate syntax for subqueries. You can not use the usual ANY keyword as this only allows you to match one column not two at the same time.
NSString *keyValue = @"title"; NSString *valueValue = @"home"; NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setEntity:[NSEntityDescription entityForName:@"Page" inManagedObjectContext:_context]]; [request setPredicate:[NSPredicate predicateWithFormat:@"(SUBQUERY(attributes, $a, $a.key == %@ && $a.value == %@).@count != 0)", keyValue, valueValue]];
更简单的谓词 ANY attributes.key =titleAND ANY attributes。 value =home将无法工作,因为它也会返回有两个例句的页面key ='addr'/ value ='home'and key ='title'/ value ='pete'。
The simpler predicate ANY attributes.key = "title" AND ANY attributes.value = "home" would not work as it also returns pages that have two dicts e.g. key='addr'/value='home' and key='title'/value='pete'.
这篇关于核心数据:通过特定属性获取(连接关系)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!