Q:

Filtering files in documents folder

Im currently searching my documents folder for .png images using
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:nil];
    fileList=[[NSMutableArray alloc]init];
    for (NSString *filename in dirContents) {
        NSString *fileExt = [filename pathExtension];
        if ([fileExt isEqualToString:@"png"]) {

            [fileList addObject:filename];
        }
    }
    NSLog(@"document folder content list %@ ",fileList);

Id like to make the search more specific, ie search for images named lorry.png, or car.png for example (for brevity) rathe rthan getting everything with .png
My image files are named by apending a reference number self.certificate.reference to the png so I get something like car283TYZ.png.   This way I can utilise the correct image for the certificate that created it.   
What ive tried
//search folder
    NSString *certRefstring = [NSString stringWithFormat:@"%@",self.certificate.reference];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:nil];
    fileList=[[NSMutableArray alloc]init];
    for (NSString *filename in dirContents) {
        NSString *fileExt = [filename pathExtension];

        if ([fileExt isEqualToString:@"png"]&&[certRefstring rangeOfString:[NSString stringWithFormat:@"%@",self.certificate.reference]].location == NSNotFound) {

            NSLog(@"string does not contain cert ref %@, file list is %@",self.certificate.reference,fileList);

        }
        else{

            NSLog(@"string contains cert ref %@ and png files %@",self.certificate.reference,fileList);

            [fileList addObject:filename];

        }
    }

    NSLog(@"document folder content list %@ ",fileList);

This seemed to return everything in the folder.  So im looking for a solution for searching more specifically/iterating over my .png files in my documents folder

A:

You should have nested 'if's
if ([fileExt isEqualToString:@"png"])
{
    if ([certRefstring rangeOfString:[NSString stringWithFormat:@"%@",self.certificate.reference]].location == NSNotFound)
    {    
        NSLog(@"string does not contain cert ref %@, file list is %@",self.certificate.reference,fileList);
    }
    else
    {
        NSLog(@"string contains cert ref %@ and png files %@",self.certificate.reference,fileList);
        [fileList addObject:filename];
    }
}

