0

我有一个字符串数组用作“过滤器”:

@[@"*.png", @".DS_Store", @"Foobar", @".jpg"]

如何使用上述模式过滤掉使用 NSPredicate 的文件夹的所有内容?

这是我到目前为止所得到的:

NSArray *contents = [fileManager contentsOfDirectoryAtPath:fullPath error:NULL];
NSArray *filter = @[@"*.png", @".DS_Store", @"Foobar", @".jpg"];
NSArray *contents = [contents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"((pathExtension IN[cd] %@) OR (lastPathComponent IN[cd] %@)) ", filter, filter]];

但这并没有给我想要的结果。例如,png文件不被过滤。另外,我不能让它不区分大小写,所以foobar不会被过滤掉。

4

2 回答 2

3

看看谓词编程指南

NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"/Users/new/Desktop/" error:nil];
NSArray *extensions = [NSArray arrayWithObjects:@"jpg", @"png",nil];
NSArray *files = [contents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"!(pathExtension IN %@) AND !(self LIKE[c] %@)", extensions, @"Foobar"]];
//[c] for case insensitive
于 2013-07-29T07:05:02.623 回答
0

您可以过滤您想要列出的内容。剩余的将自动过滤。

-(void)filter
{
    localSongs=[[NSMutableArray alloc]init];
    NSArray * paths = NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory,  NSUserDomainMask, YES);
    documentsDirectory = [paths objectAtIndex:0];
    NSDirectoryEnumerator *de = [[NSFileManager defaultManager]   enumeratorAtPath:documentsDirectory] ;
    NSString *file;

    while ((file = [de nextObject]))
    if (([[file pathExtension] isEqualToString:@"MP3"]) || ([[file  pathExtension]isEqualToString:@"mp4"]) ||  ([[file pathExtension]isEqualToString:@"mp3"]) ||  ([[file pathExtension]isEqualToString:@"mov"]) || ([[file  pathExtension]isEqualToString:@"avi"]) )

    [localSongs addObject:file.lastPathComponent];


    NSLog(@"local files%@",localSongs);
}
于 2013-07-29T06:34:13.677 回答