NSPredicate 以多个文件结尾
我正在尝试使用谓词检查以一组扩展名结尾的文件来过滤数组。我怎样才能做到呢?
类似于 'selfendswith in %@' 的东西会起作用吗?谢谢!
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]
]];
I am trying to filter an array using a predicate checking for files ending in a set of extensions. How could I do it?
Would something close to 'self endswith in %@' work? Thanks!
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]
]];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要包含数组,而是需要 in。理想情况下,您还希望按路径扩展名进行过滤。所以
You don't want contains for an array, you want in. You also ideally want to filter by the path extension. So
编辑马丁的答案比这个要好得多。他的答案就是正确的。
有几种方法可以做到这一点。也许最简单的就是构建一个巨大的 OR 谓词:
这将创建一个相当于以下内容的谓词:
edit Martin's answer is far superior to this one. His is the correct answer.
There are a couple ways to do it. Probably the simplest would just be to build a giant OR predicate:
This will create a predicate that's equivalent to: