how to find multiple files with wildcard expressio

2019-02-20 16:42发布

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条回答
女痞
2楼-- · 2019-02-20 17:18

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;
}
查看更多
登录 后发表回答