Get the List Of Specified File Types (png, xml, etc) In Any Folder In iOS - Online Free Computer Tutorials.

'Software Development, Games Development, Mobile Development, iOS Development, Android Development, Window Phone Development. Dot Net, Window Services,WCF Services, Web Services, MVC, MySQL, SQL Server and Oracle Tutorials, Articles and their Resources

Monday, June 18, 2012

Get the List Of Specified File Types (png, xml, etc) In Any Folder In iOS

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.


I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too. 

Stay tuned to my blogtwitter 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.


No comments:

Post a Comment