how to find multiple files with wildcard expression in objective-c

◇◆丶佛笑我妖孽 提交于 2019-12-01 10:55:18

问题


How do I get hold of all the *.bmp files (or *.xyz in general) in my Document folder, writing an iPhone 3.0 SDK?


回答1:


You need to use NSFileManager's contentsOfDirectoryAtPath:error: function. Note that this does not traverse subdirectories and extension cannot contain a ..

-(NSArray *)findFiles:(NSString *)extension
{
    NSMutableArray *matches = [[NSMutableArray alloc]init];
    NSFileManager *manager = [NSFileManager defaultManager];

    NSString *item;
    NSArray *contents = [manager contentsOfDirectoryAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] error:nil];
    for (item in contents)
    {
        if ([[item pathExtension]isEqualToString:extension])
        {
            [matches addObject:item];
        }
    }

    return matches;
}


来源:https://stackoverflow.com/questions/4764323/how-to-find-multiple-files-with-wildcard-expression-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!