iPhone/iOS: How to list all files in a resource group?
I've got a bunch of resources -- images, to be specific -- which are all stuck in a resource group via XCode.
I know 开发者_运维技巧how to load a specific image file from within that resource group by specifying the filename and using inDirectory to specify the "path" to the group it's in.
But is it possible to obtain (programatically) a list of all the files in a certain group? The app I'm building displays a random image from among all the ones in a specific folder. While I could obviously just create a hard-coded array containing a list of all those files, it would be pretty convenient to be able to add more files just by dropping them into the folder.
So: Can it be done?
Thanks!
What about using the pathsForResourcesOfType:inDirectory:
method of the NSBundle
class?
It will return you a NSArray with the path of files matching the given type.
Try This:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
//--- Listing file by name sort
NSLog(@"\n File list %@",fileList);
//---- Sorting files by extension
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF EndsWith '.png'"];
filePathsArray = [filePathsArray filteredArrayUsingPredicate:predicate];
NSLog(@"\n\n Sorted files by extension %@",filePathsArray);
精彩评论