【发布时间】:2012-05-26 05:37:25
【问题描述】:
我希望我的用户能够将文件夹拖放到我的 OSX 应用程序中。然后我查看目录及其子目录中的所有文件。这很好用,但当目录中有别名时就不行了。别名已正确解析(感谢 Matt Gallagher,Apple 真丢脸),但枚举器不允许我枚举目标目录(它只是不返回任何元素和下面的错误)。下面是一些示例代码:
-(void)enumDirAtPath:(NSString*)path {
NSString* file_enum = nil;
NSDirectoryEnumerator* enumerator = [[NSFileManager defaultManager] enumeratorAtPath:path];
while (file_enum = [enumerator nextObject])
{
NSString* file = [[NSString stringWithFormat:@"%@/%@",path,file_enum] stringByResolvingSymlinksAndAliases];
BOOL isDirectory = NO;
[[NSFileManager defaultManager] fileExistsAtPath:file isDirectory: &isDirectory];
if (!isDirectory) {
NSLog(@"Adding file: %@",file);
} else {
NSLog(@"Found dir: %@",file);
[self enumDirAtPath: file];
}
}
}
与enumeratorAtURL:includingPropertiesForKeys:options:errorHandler: 相同的代码给了我这个错误:
错误:错误域=NSCocoaErrorDomain 代码=257“文件“音乐” 无法打开,因为您无权查看。” 用户信息=0x1094cf670 {NSURL=file://localhost/Users/david/Desktop/Musik, NSFilePath=/Users/david/Desktop/Musik, NSUnderlyingError=0x1094d43f0 "操作无法完成。操作不允许"}
即使我只是 dispatch_async 递归调用,它也不会让我这样做。有什么方法可以遍历目录并获取所有内容?
注意:我完全清楚这可能会导致无限循环。这段代码只是为了说明问题。
【问题讨论】:
-
您可以而且应该使用
stringByAppendingPathComponent:而不是将路径与stringWithFormat:一起使用。
标签: cocoa nsfilemanager