-2

I am reading files from folder, folder contains .pngs but I get additionally Thumb.db file in every folder and all others are .pngs. I read content as

NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];

How to remove all files which contains .db in array or to stay just which ends with .png ?

Ekta Padaliya
  • 5,743
  • 3
  • 39
  • 51
PaolaJ.
  • 10,872
  • 22
  • 73
  • 111

3 Answers3

4

Idiomatic way of filtering arrays in Cocoa is with NSPredicate:

NSArray *filtered = [directoryContent filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"pathExtension = png"]];

pathExtension method mentioned inside the predicate is a method on NSString that tries to interpret the string as a filesystem path, and return the extension.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
4

I think by using NSPredicate method you can get exact values

NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
NSString *str              = @".png"
NSPredicate *sPredicate    = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",str];
NSArray *predicateArray    = [directoryContent filteredArrayUsingPredicate:sPredicate];
0

Try this code:

    NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
    NSMutableArray *imageArray = [[NSMutableArray alloc] init];

    for (NSString *path in directoryContent) 
    {
       if ([path hasSuffix:@".png"]) 
       {
          [imageArray addObject: path];
       }
    }

    NSLog (@"Number of images : %d" , [imageArray count]);
Mrunal
  • 13,982
  • 6
  • 52
  • 96