Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

knott

macrumors newbie
Original poster
Nov 24, 2009
4
0
Hello all,

I have a file with a fixed name "dog" in the resource dir. This file could be "dog.jpg", "dog.png", "dog.gif", "dog.txt", "dog.wav". I would like to find file based on the extension: If "dog.jpg" does not exist, then it should look for the next "dog.png" and so on. I mean something like NSArray *fileTypes below with only 3 defined filetypes to looking for. Any help would be much appreciated, Thank you!

PHP:
NSBundle *bundle;
NSString *Path;
bundle = [NSBundle mainBundle];
//NSArray *fileTypes = [NSArray arrayWithObjects:@"jpg", @"gif", @"png", nil];
Path = [bundle pathForResource:@"dog" ofType:fileTypes];
	if (!Path) {
		NSLog(@"ERROR: Cannot find the dog file.\n");
	} else {
        NSLog(@"FOUND: Filename is: %@\n", Path);
        }
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
How about a loop?
(untested)
Code:
NSBundle *bundle = [NSBundle mainBundle];
NSArray *fileTypes = [NSArray arrayWithObjects:@"jpg", @"gif", @"png", nil];
NSString *path = nil;
for (NSString *type in fileTypes) {
    path = [bundle pathForResource:@"dog" ofType:type];
    if (path)
        break;
}
if (path)
    NSLog(@"FOUND: Filename is: %@\n", path);
else
    NSLog(@"ERROR: Cannot find the dog file.\n");
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.