NSPredicate endswith multiple files

后端 未结 2 1116
走了就别回头了
走了就别回头了 2020-12-03 14:12

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 endswi

2条回答
  •  猫巷女王i
    2020-12-03 14:25

    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]];
    

提交回复
热议问题