0

我有实体: Language、Proper 和 Answer

模型看起来像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}

//make 并将 NSPredicate 设置为数组控制器 //数组控制器排列对象将返回 Java

抱歉语法不好:/

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

4

2 回答 2

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
}
于 2013-10-08T08:26:04.597 回答
1

您需要来自 SUBQUERY。就像是:

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

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

您可以使用 and "ANY ...",但并非在所有情况下都有效。

于 2013-10-08T07:39:07.527 回答