【问题标题】:Creating an array from documentsDirectory includes ends with .MOV从documentsDirectory 创建一个数组包括以.MOV 结尾
【发布时间】:2011-09-16 15:47:46
【问题描述】:

我想从我的应用沙箱的文档目录中创建一个 NSArray。它包含很多文件,但我的数组只会以 .MOV 结尾。

你能帮帮我吗?

谢谢!

【问题讨论】:

    标签: objective-c


    【解决方案1】:

    使用NSFileManager 有几个选项。

    NSString *path = @"<dir_path>";
    
    //Option 1 using directory enumerator
    NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager] enumeratorAtPath:path];
    NSMutableArray *movfiles = [NSMutableArray array];
    while(NSString *file = [direnum nextObject])
    {
        if([[file pathExtension] isEqualToString:@"MOV"])
            [movfiles addObject:movfiles];
    }
    

    //Option 2 case insensitive using a predicate
    NSError *error;
    NSArray *dircontents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];
    if(error)
    {
        //Handle error
    }
    else
    {
        dircontents = [dircontents filteredArrayUsingPredicate:
                       [NSPredicate predicateWithFormat:@"pathExtension ==[c] %@", @"mov"]];
    }
    

    【讨论】:

    • 我将让您在documentation 中自己发现这个。这就是我回答这个问题时所做的一切:)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 2022-01-18
    • 1970-01-01
    • 2016-07-06
    • 2012-07-30
    • 1970-01-01
    • 2015-08-28
    相关资源
    最近更新 更多