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

I am developing an application for iPhone and iPad with the latest versions of the SDK and Xcode 4.2.

I want to get the path for all resources whose names begin with 001 . Their name is 001XXX.jpg ( XXX is unknown). I do not know how many resources.

For example, I will have a package in the application of the following files:

001001.jpg
001002.jpg
001003.jpg

I want to get an NSArray with an outline for each of these three files.

+6
source share
2 answers

You can do this in two steps:

The code (not tested) might look like this:

NSArray *files = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:@"."]; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[c] '001'"]; NSArray *images = [files filteredArrayUsingPredicate:pred]; 
+3
source

We hope you find the code below useful.

 NSArray *dirContents = [[NSFileManager defaultManager] directoryContentsAtPath:documentDirectory]; for (NSString *tString in dirContents) {  if ([tString hasPrefix:@"001"] && [tString hasSuffix:@".jpg"]) {    // do stuff  } } 

Here documentDirectory denotes the path to the document directory. If your files are located in a location other than the document directory, obviously you can put the path there.

0
source

Source: https://habr.com/ru/post/907203/


All Articles