NSPredicate:CoreData fetchRequest按字典数组过滤 [英] NSPredicate : CoreData fetchRequest filtered by Array of dictionaries

查看:233
本文介绍了NSPredicate:CoreData fetchRequest按字典数组过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用字典数组创建NSPredicate?



我有以下结构:

  [{name:foo,city:Paris},{name:bar,city:London}] 

我想通过这些对过滤我的 NSFetchRequest (假设属性在CoreData中具有相同的名称)



当传递数组时,我可以使用关键字 IN 。但我不知道如何使这个工作与一个字典数组。

解决方案

我不认为你将能够使用 IN ,因此您需要使用一些 OR 。大纲:


  1. 迭代数组的所有元素。

  2. dictionary ),构造一个形式的谓词:

      predicateWithFormat:@name ==%@ AND city ==%@,[dictionary objectForKey:@name],[dictionary objectForKey:@city]]; 


  3. 将每个此类谓词添加到NSMutableArray(数组

    p> [NSCompoundPredicate orPredicateWithSubpredicates:array]


如果性能是一个问题,请考虑使用替换变量而不是格式来构建单个谓词。


Is it possible to create an NSPredicate using an array of dictionaries ?

I have a following structure :

[{ name: "foo", city:"Paris"},{name:"bar", city:"London"}]

An I want to filter my NSFetchRequest by these pairs. (supposing the properties have the same names in CoreData)

When passing an array I can use the keyword IN. But I don't get how make this work with an array of dictionaries.

解决方案

I don't think you will be able to use IN, so you need to use a number of ORs. In outline:

  1. Iterate through all elements of the array.
  2. For each element (dictionary), construct a predicate of the form:

    [NSPredicate predicateWithFormat:@"name == %@ AND city == %@",[dictionary objectForKey:@"name"], [dictionary objectForKey:@"city"]];
    

  3. Add each such predicate to a NSMutableArray (array) of predicates

  4. Build a compound predicate from the array using:

    [NSCompoundPredicate orPredicateWithSubpredicates:array]
    

If performance is an issue, consider building the individual predicates with substitution variables rather than with format.

这篇关于NSPredicate:CoreData fetchRequest按字典数组过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆