使用NSPredicate过滤数组数组 [英] Using NSPredicate to filter array of arrays

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

问题描述

我有以下情况:

NSArray(
    NSArray(
        string1,
        string2,
        string3,
        string4,
        string5,
    )
    ,
    NSArray(
        string6,
        string7,
        string8,
        string9,
        string10,
   )
)

现在我需要一个返回包含特定字符串的数组的谓词。
,例如包含string9的Filter Array - >我应该返回整个第二个数组,因为我需要处理该数组中的其他字符串。有什么想法?

Now I need a predicate that returns the array that contains a specific string. e.g. Filter Array that contains string9 -> I should get back the entire second array because I need to process the other strings inside that array. Any ideas?

推荐答案

为了完整性:可以使用完成predicateWithFormat:

NSArray *array = @[
    @[@"A", @"B", @"C"],
    @[@"D", @"E", @"F"],
];

NSString *searchTerm = @"E";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SELF == %@", searchTerm];
NSArray *filtered = [array filteredArrayUsingPredicate:predicate];
NSLog(@"%@", filtered);

输出:


(
    (
        D,
        E,
        F
    )
)

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

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