【问题标题】:iOS: Search for video files in cashes folder and each subfolderiOS:在 Cashes 文件夹和每个子文件夹中搜索视频文件
【发布时间】:2013-01-08 06:15:04
【问题描述】:

是否可以在一个文件夹及其所有子文件夹中搜索某种类型的所有文件(例如 mp4)?到目前为止,我可以使用此代码一次为一个文件夹执行此操作:

NSFileManager *fm = [NSFileManager defaultManager];
NSArray *dirContents = [fm contentsOfDirectoryAtPath:cachesPath error:nil];
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.mp4'"];
NSArray *filesWithAttr = [dirContents filteredArrayUsingPredicate:fltr];

NSLog(@"%@", filesWithAttr);

但我的 cashes 文件夹中有很多子文件夹。我是否必须逐一检查并检查 mp4 文件还是有更简单的解决方案?

提前致谢

【问题讨论】:

  • 看看[fm fileExistsAtPath:yourPath isDirectory:&foo];[fm subpathsAtPath:yourPath];
  • 正是我所需要的。非常感谢

标签: ios search directory subdirectory


【解决方案1】:

您可以通过获取所有子路径然后过滤它们来轻松实现此目的。所以用subpathsOfDirectoryAtPath替换你的contentsOfDirectoryAtPath,它应该可以工作。该函数进行深度枚举,因此所有子目录也包括在内。

NSFileManager *fm = [NSFileManager defaultManager];
NSArray *dirContents = [fm subpathsOfDirectoryAtPath:cachesPath error:nil];
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.mp4'"];
NSArray *filesWithAttr = [dirContents filteredArrayUsingPredicate:fltr];

功能说明: Apple iOS Developer Library

【讨论】:

    猜你喜欢
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 2017-02-12
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多