Here we are going to show how you can get a list of file types using the NSPredicate object.
For example, let's you need a list of all the PNG files available in your application bundle, then
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *bundleDirectory = [fileManager contentsOfDirectoryAtPath:bundlePath error:nil];
NSPredicate *filter = [NSPredicate predicateWithFormat:@"self ENDSWITH '.png'"];
NSArray *pngFiles = [bundleDirectory filteredArrayUsingPredicate:filter];
NSLog(@"PNG files: %@", pngFiles);
Here the output is like,
2012-06-18 12:36:16.869 Sandbox[23412:13a03] PNG files: (
"default.png",
"photo1.png",
"photo2.png",
"myImage.png"
)
You can change the path and filter as needed to list files of any type and in any folder.
Stay tuned to my blog, twitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.
For example, let's you need a list of all the PNG files available in your application bundle, then
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *bundleDirectory = [fileManager contentsOfDirectoryAtPath:bundlePath error:nil];
NSPredicate *filter = [NSPredicate predicateWithFormat:@"self ENDSWITH '.png'"];
NSArray *pngFiles = [bundleDirectory filteredArrayUsingPredicate:filter];
NSLog(@"PNG files: %@", pngFiles);
Here the output is like,
2012-06-18 12:36:16.869 Sandbox[23412:13a03] PNG files: (
"default.png",
"photo1.png",
"photo2.png",
"myImage.png"
)
You can change the path and filter as needed to list files of any type and in any folder.
Stay tuned to my blog, twitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.
0 Comments