我有一个字符串数组用作“过滤器”:
@[@"*.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
不会被过滤掉。
最佳答案
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
关于objective-c - 使用 NSPredicate 过滤 NSFileManger 内容以获取文件 'patterns' 的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17917251/