如何通过带有键的嵌套数组进行谓词化 [英] How to Predicate through Nested Arrays with Keys

查看:83
本文介绍了如何通过带有键的嵌套数组进行谓词化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 self.eventsArray 的数组,如下所示:

I have an array called self.eventsArray which looks like this:

[ 
  {
        "artist": [
            {
                "artist_name": "Dillon Francis", 
                "name": "Dillon Francis", 
            }, 
            {
                "artist_name": "Major Lazer", 
                "name": "Major Lazer", 
            }, 
            {
                "artist_name": "Flosstradamus ", 
                "name": "Flosstradamus", 
            }
        ],  
        "name": "Mad Decent Block Party NYC", 
    },
  {
        "artist": [
            {
                "artist_name": "Ryan Raddon", 
                "name": "Kaskade", 
            }
        ], 
        "name": "Kaskade Atmosphere Tour NYC", 
    },
]

我一直在尝试使用 NSPredicate 来过滤它我的时代。当用户搜索时,需要按名称(在艺术家数组中)或名称(在顶级数组中)进行过滤。

I have been trying to filter it by using NSPredicate two times. When the user searches, it needs to be filtered by either name (in the artist array) or name (in the top-level array).

NSPredicate *predicateEventsByName = [NSPredicate predicateWithFormat:@"SELF.%K contains[c] %@",@"name",searchText];
NSPredicate *predicateEventsByArtistName = [NSPredicate predicateWithFormat:@"SELF.%K.%K contains[c] %@",@"artist",@"name",searchText];

NSMutableArray *unfilteredEventsArray = [[NSMutableArray alloc] initWithCapacity:0];
unfilteredEventsArray = [NSMutableArray arrayWithArray:[self.eventsArray filteredArrayUsingPredicate:predicateEventsByName]];
[unfilteredEventsArray addObjectsFromArray:[self.eventsArray filteredArrayUsingPredicate:predicateEventsByArtistName]];


 [self.filteredEventsArray addObjectsFromArray:[[NSSet setWithArray:unfilteredEventsArray] allObjects]];

使用此代码, self.filteredEventsArray 将填充与顶级name匹配的任何搜索词。它不会为嵌套艺术家提供任何搜索结果name
我怀疑它不会搜索嵌套数组的原因是因为这行:

Using this code, self.filteredEventsArray will be populated with any search term matching the top-level "name". It won't give any search results for the nested artist "name". I suspect the reason why it won't search through the nested array is because of this line:

NSPredicate *predicateEventsByArtistName = [NSPredicate predicateWithFormat:@"SELF.%K.%K contains[c] %@",@"artist",@"name",searchText];

但我无法弄清楚如何更改 predicateWithFormat:使其搜索嵌套数组。

but I can't figure out how to change the predicateWithFormat: to make it search through the nested array.

推荐答案

艺术家是一个数组,因此你必须在谓词中使用ANY:

"artist" is an array, therefore you have to use "ANY" in the predicate:

[NSPredicate predicateWithFormat:@"ANY %K.%K CONTAINS[c] %@",
              @"artist",@"name",searchText];

请注意,不是手动构建搜索结果的联合,而是b $ b使用复合谓词:

Note that instead of building the "union" of the search results manually, you can use a "compound predicate":

NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:
                          @[predicateEventsByName, predicateEventsByArtistName]];
self.filteredEventsArray = [self.eventsArray filteredArrayUsingPredicate:predicate];

这篇关于如何通过带有键的嵌套数组进行谓词化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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