1

我有简单的代码:

NSFileManager *fileManager = [NSFileManager defaultManager];

if (!fileManager) NSLog(@"Manager doesn't exist!");

if(![fileManager fileExistsAtPath:destString]) {
    if (![fileManager createDirectoryAtPath:destString withIntermediateDirectories:YES attributes:nil error:&error]){
        NSLog(@"%@", [error localizedFailureReason]);
    }
}
else NSLog(@"Exists!");

变量:

destString = file://localhost/Users/SOMEUSER/Desktop/NEWFOLDER/

当我试图创建一个文件夹时,程序会写“存在”但在桌面上不存在。当我删除 fileExistsAtPath: 时,没有错误,但也没有目录。谢谢 4 回复!

4

1 回答 1

4

-createDirectoryAtPath:withIntermediateDirectories:attributes:error:将要创建的路径作为 UNIX 样式的路径字符串,而不是文件 URL 样式的字符串。也就是说,你想给它传递一个像/Users/SOMEUSER/Desktop/NEWFOLDER/.

或者,如果您正在处理 URL 样式的字符串,那么您可以改用使用并从您的字符串-createDirectoryAtURL:withIntermediateDirectories:attributes:error:构造一个。NSURL

于 2013-02-13T09:58:25.760 回答