NSPredicate过滤子对象返回父对象

时间:2015-03-10 11:00:59

标签: objective-c arrays nspredicate

我试图在NSArray表单中过滤以下JSON对象示例。

[
   {
       "Name": "Andrew",
       "Age": 28,
       "Pets": [
          {
              "Name": "Domino"
          },
          {
              "Name": "Sheldon"
          }
       ]
   },
   {
       "Name": "Matthew",
       "Age": 26,
       "Pets": [
          {
              "Name": "Shadow"
          },
          {
              "Name": "Mojo"
          }
       ]
   }
]

我想过滤Pets.Name包含字符串"Sha"

的位置

鉴于这个谓词,我希望结果匹配为:

[
   {
      "Name":"Matthew",
      "Age":26,
      "Pets":[
         {
            "Name":"Shadow"
         }
      ]
   }
]

这是一种简单的方法吗?

 NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"Pets.Name == %@", searchText];
 NSArray *temp = [MY_ARRAY filteredArrayUsingPredicate:filterPredicate];

1 个答案:

答案 0 :(得分:0)

使用ANY SQL命令,如下所示:

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"ANY Pets.Name == %@", searchText];