【问题标题】:Folders (directories) not being detected properly未正确检测到文件夹(目录)
【发布时间】:2009-10-17 20:23:09
【问题描述】:

我注意到“沼泽标准”Objective-C 文件夹检测代码存在错误。我正在扫描具有给定路径的文件和文件夹,并计算存在多少文件和存在多少文件夹。

奇怪的是,我返回的文件夹数量比实际存在的多一个!

使用调试单步执行例程并观察每个文件/文件夹名称,因为例程确定对象是文件还是文件夹,这表明其中一个文件通过了作为文件夹的测试!?!?! ?

被错误检测到的文件是“带附件的 RTF (RTFD)”文件类型。我还没有检查其他文件夹,看看是否有更多的文件类型可能会错误地报告自己。

除了这个文件之外,我的程序中的其他一切都正常工作。

有没有人知道我可能做错了什么?或者它是 Objective-C 中的一个已知错误?

这是我正在使用的部分代码:

 BOOL isDir;
    NSString *file;
 NSString *docsDir = [self path];
 NSFileManager *manager = [NSFileManager defaultManager];
 NSDirectoryEnumerator *dirEnum = [manager enumeratorAtPath: docsDir];
 NSDictionary *fattrs;
        //(only showing important declarations above)

while (file = [dirEnum nextObject]) {
    //If user clicked the Abort Button, get out of the loop
    if (abortFlag)
        break;

    if ([excludeSubdirectories state] == NSOnState) {
        [dirEnum skipDescendents];
    }

    if ([manager fileExistsAtPath:[docsDir stringByAppendingPathComponent:file]
                      isDirectory:&isDir] && isDir) {
       ++dirCount;
       if ([excludeSubdirectories state] == NSOnState) {
           continue;
       }
    }
}
            //... Do a bunch of other stuff, etc., etc. ...

【问题讨论】:

    标签: objective-c directory


    【解决方案1】:

    RTFD 文件实际上是捆绑包,只是花哨的目录。您可以通过右键单击您的 rtfd 文件并选择“显示包内容”选项来验证这一点。

    如果您在 Mac OS X 上编写此代码,则可以使用 -[NSWorkspace isFilePackageAtPath:] 来识别这些捆绑包。

    【讨论】:

    • 噢噢噢——说得通!非常感谢您快速简单的回答! (是的......它在 OS X 上)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    相关资源
    最近更新 更多