NSPredicate对许多人来说像< ----->> b -----&以及c

时间:2013-10-08 04:24:21

标签: objective-c core-data nspredicate

我有实体:语言,正确和答案

模型看起来像Language{A:name(NSString), R:propers(NSSet)} --->> Proper{A:name(NSString), R:answer(Answer)} ---> Answer{A:answer(NSString)}

所以,我得到了带有参数的NSDictionary:{@"key1", @"value1"}, {@"key2", @"value2"}... i

我需要从此词典中创建 NSPredicate ,以便从我的NSDictionary中获取所有语言,其中propers.name = key[i]propers.answer.answer = value[i]

示例:

C++

level : high

try/catch : yes

typization : static

Java

level : high

try/catch : yes

typization : dynamic

NSDictionary : {level : hight}, {try/catch : yes}, {typization : dynamic}

//制作NSPredicate并将其设置为数组控制器 //数组控制器arrangeObjects将返回Java

抱歉语法不好:/

更新 * 经过2个星期的不眠之夜和专家系统老师的工作,没有检查它就去了实验室。杀了我算了。非常感谢大家。 *

2 个答案:

答案 0 :(得分:1)

您需要来自SUBQUERY。类似的东西:

[NSPredicate predicateWithFormat:@"B.key = %@ AND SUBQUERY(B, $B, $B.C.value = %@).@count > 0", key, value];

SUBQUERY遍历对象,如果你有多个多对多的关系,你也可以拥有嵌套的SUBQUERY。

您可以使用和“ANY ...”但它并不适用于所有情况。

答案 1 :(得分:1)

我只想猜你想做什么,所以这里是一个代码:

- (NSPredicate *)constructPredicateWithDictionary:(NSDictionary *)dictionary
{
    NSArray *allKeys = [dictionary allKeys];
    NSMutableArray *predicates = [NSMutableArray array];

    for (NSString *key in allKeys) {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(B, $B, $B.key = %@ && $B.C.value = %@).@count > 0", key, [dictionary valueForKey:key]];
        [predicates addObject:predicate];
    }


    //not quite sure what you need so I am guessing

    NSPredicate *finalAndPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicates]; //if you want all the predicates to be concatenated with and '&&' - logical expression - so all of the subqueries have to be correct
    NSPredicate *finalOrPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:predicates]; //if you want all the predicates to be concatenated with or '||' - logical expression - so any of the subqueries has to be correct

    return finalOrPredicate; //return the needed predicate
}