Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting all resources with a name starting with 001XXX.jpg

I'm developing an iPhone and iPad application with latest SDK and XCode 4.2.

I want to retrieve the path for all resources which name start with 001. Their name is 001XXX.jpg (XXX is unknown). I don't know how many resources are.

For example, I will have bundle in app the following files:

001001.jpg
001002.jpg
001003.jpg

I want to get a NSArray with the path for each of this three files.

like image 669
VansFannel Avatar asked Jan 30 '12 07:01

VansFannel


1 Answers

You can you do it in two steps:

  • Use the NSBundle pathsForResourcesOfType:inDirectory: method to retrieve all the png files.
  • Use the NSArray filteredArrayUsingPredicate: method to filter out the pattern you want.

The code (not tested) could look like:

NSArray *files = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:@"."];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[c] '001'"];
NSArray *images = [files filteredArrayUsingPredicate:pred];
like image 190
Laurent Etiemble Avatar answered Nov 06 '22 19:11

Laurent Etiemble