【问题标题】:CreateDirectoryAtPath: doesn't workCreateDirectoryAtPath:不起作用
【发布时间】:2013-02-13 09:53:00
【问题描述】:

我有简单的代码:

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 回复!

【问题讨论】:

    标签: objective-c cocoa nsfilemanager


    【解决方案1】:

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

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

    【讨论】:

    • 问题已解决!谢谢bdash!
    猜你喜欢
    • 1970-01-01
    • 2011-06-22
    • 2023-03-21
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    相关资源
    最近更新 更多