使用Cocoa NSFIleManager忽略文件夹中的.DS_Store和Icon文件

min*_*pop 14 cocoa icons parsing objective-c nsfilemanager

我正在尝试使用NSFileManager从目录中删除特定文件.我想忽略目录中隐藏的.DS_Store和Icon文件(我正在检查的文件夹必须有一个自定义图标),但我也不小心删除了它们.现在,我正在做以下事情:

 NSFileManager *manager = [NSFileManager defaultManager];
 NSArray *dirContents = [manager contentsOfDirectoryAtPath:[selectedFolder stringValue] error:nil]; 
 for (int i = 0; i < [dirContents count]; i++)
 {
     NSString *theFile = [dirContents objectAtIndex:i];

     if([theFile isEqualToString:@".DS_Store"] || [theFile isEqualToString:@"Icon?"] || [theFile isEqualToString:@"Icon"])
     { 
        continue;
     }
     //do manipulations on files here
 }
[manager release];
Run Code Online (Sandbox Code Playgroud)

但是,我的if语句中没有匹配.DS_Store和Icon文件.此外,当我在Finder中显示隐藏文件时,图标文件称为"图标".但是,在终端的该目录中执行ls会打印出"Icon?".

如何正确解析我的代码中的这些文件?

谢谢

编辑: 所以它实际上是成功忽略.DS_Store文件,但Icon文件仍然超过if语句.

Jos*_*ell 25

有趣的是,我认为最近发布另一个问题问题部分基本上回答了你的问题.如果您使用:

-[NSFileManager contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:] 
Run Code Online (Sandbox Code Playgroud)

(doc link),您可以传递一个选项,NSDirectoryEnumerationSkipsHiddenFiles以忽略隐藏文件,这样您就不必检查特定的文件:

NSURL * selectedFolderURL = [NSURL fileURLWithPath:[selectedFolder stringValue]];
[myFileManager contentsOfDirectoryAtURL:selectedFolderURL
             includingPropertiesForKeys:[NSArray arrayWithObject:NSURLNameKey]
                                options:NSDirectoryEnumerationSkipsHiddenFiles
                                  error:&error];
Run Code Online (Sandbox Code Playgroud)

请注意,这将返回绝对URL,而您的问题中的方法将返回对于原始目录的路径.很容易解决,但重要的是要知道,如果你删除的东西.