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

后端 未结 1 892
生来不讨喜
生来不讨喜 2021-01-16 10:13

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条回答
  • 2021-01-16 10:57

    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;
    }
    
    0 讨论(0)
提交回复
热议问题