Find file in resource dir

Discussion in 'Mac Programming' started by knott, Dec 8, 2009.

  1. knott macrumors newbie

    Joined:
    Nov 24, 2009
    #1
    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);
            }

     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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");
     
  3. knott thread starter macrumors newbie

    Joined:
    Nov 24, 2009

Share This Page