how to find multiple files with wildcard expression in objective-c
How do I get hold of all开发者_StackOverflow the *.bmp files (or *.xyz in general) in my Document folder, writing an iPhone 3.0 SDK?
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;
}
精彩评论