如何过滤多级数组来搜索字符串?

时间:2017-07-04 06:48:45

标签: objective-c nspredicate

NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@"ANY %K.%K.%K CONTAINS[c] %@",@"Assets",@"TFloorAssets",@"AssetNo",strSearch];

NSArray *filteredPendingList = [NSMutableArray arrayWithArray:[arrOfAllAssetsCategory filteredArrayUsingPredicate:predicate3]];

我使用上述类型的代码进行多级搜索?

2 个答案:

答案 0 :(得分:2)

使用collection operator

[NSPredicate predicateWithFormat:@"ANY Assets.@unionOfArrays.TFloorAssets.AssetNo contains[c] %@", strSearch]

subquery

[[NSPredicate predicateWithFormat:@"SUBQUERY(Assets, $asset, ANY $asset.TFloorAssets.AssetNo contains[c] %@).@count > 0", strSearch]

或两个子查询:

[NSPredicate predicateWithFormat:@"SUBQUERY(Assets, $asset, SUBQUERY($asset.TFloorAssets, $tFloorAsset, $tFloorAsset.AssetNo contains[c] %@).@count > 0).@count > 0", strSearch]

答案 1 :(得分:-1)

使用“复合谓词”,如下所示

NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:
                          @[predicateEventsByAsset, predicateEventsByFloorAsset ,predicateEventsByAssetNo]];
self.filteredEventsArray = [self.eventsArray filteredArrayUsingPredicate:predicate];