Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSPredicate endswith multiple files

I am trying to filter an array using a predicate checking for files ending in a set of extensions. How could I do it?

Would something close to 'self endswith in %@' work? Thanks!

NSArray * dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectoryPath error:nil];
NSArray * files = [dirContents filteredArrayUsingPredicate:
    [NSPredicate predicateWithFormat:@"self CONTAINS %@",
    [NSArray arrayWithObjects:@".mp4", @".mov", @".m4v", @".pdf", @".doc", @".xls", nil]
    ]];
like image 650
Soch S. Avatar asked Feb 17 '11 17:02

Soch S.


1 Answers

You don't want contains for an array, you want in. You also ideally want to filter by the path extension. So

NSArray *extensions = [NSArray arrayWithObjects:@"mp4", @"mov", @"m4v", @"pdf", @"doc", @"xls", nil];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectoryPath error:nil];
NSArray *files = [dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"pathExtension IN %@", extensions]];
like image 109
Martin Pilkington Avatar answered Oct 11 '22 22:10

Martin Pilkington