使用文件管理器新目录不起作用?

时间:2018-11-26 06:39:34

标签: ios swift

这是我的代码,但是此代码无法正常工作,这是我的权限错误。

   let fileManager = FileManager.default

    // Create 'subfolder' directory

    do {
        try fileManager.createDirectory(atPath: "subfolder", withIntermediateDirectories: true, attributes: nil)
    }
    catch let error as NSError {
        print("Ooops! Something went wrong: \(error)")
    }

    let documentsUrl:URL =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL!

    print("\(documentsUrl)")

**错误**

  

糟糕!发生问题:错误域= NSCocoaErrorDomain代码= 513   “您无权保存文件“子文件夹”。”

1 个答案:

答案 0 :(得分:1)

我认为,您应该提供新目录的完整路径,如下所示。

let applicationDocumentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last!

    let fileManager = FileManager.default

    do {
        try fileManager.createDirectory(atPath: applicationDocumentsDirectory + "/subfolder", withIntermediateDirectories: true, attributes: nil)
    }
    catch let error as NSError {
        print("Ooops! Something went wrong: \(error)")
    }