NSPredicate以多个文件结尾 [英] NSPredicate endswith multiple files

查看:139
本文介绍了NSPredicate以多个文件结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用谓词检查以一组扩展名结尾的文件来过滤数组。我怎么能这样做?

I am trying to filter an array using a predicate checking for files ending in a set of extensions. How could I do it?

接近'self的内容会在%@'工作吗?谢谢!

NSArray * dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectoryPath error:nil];
NSArray * files = [dirContents filteredArrayUsingPredicate:
    [NSPredicate predicateWithFormat:@"self CONTAINS %@",
    [NSArray arrayWithObjects:@".mp4", @".mov", @".m4v", @".pdf", @".doc", @".xls", nil]
    ]];


推荐答案

你不希望包含数组,你想要进入。理想情况下,您还希望按路径扩展名进行过滤。所以

You don't want contains for an array, you want in. You also ideally want to filter by the path extension. So

NSArray *extensions = [NSArray arrayWithObjects:@"mp4", @"mov", @"m4v", @"pdf", @"doc", @"xls", nil];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectoryPath error:nil];
NSArray *files = [dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"pathExtension IN %@", extensions]];

这篇关于NSPredicate以多个文件结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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