1

在我的 macOS 应用程序中,我正在尝试使用以下扩展名创建目录

extension URL {
    static func createFolder(folderName: String, folderPath:URL) -> URL? {
        let fileManager = FileManager.default
        let folderURL = folderPath.appendingPathComponent(folderName)
        // If folder URL does not exist, create it
        if !fileManager.fileExists(atPath: folderURL.path) {
            do {
                // Attempt to create folder
                // try fileManager.createDirectory(at: folderURL, withIntermediateDirectories: true, attributes: nil)
                // try fileManager.createDirectory(atPath: folderURL.path, withIntermediateDirectories: true, attributes: nil)
                try fileManager.createDirectory(atPath: folderURL.relativePath, withIntermediateDirectories: true, attributes: nil)
            } catch {
                print(error.localizedDescription + ":\(folderURL.path)")
                return nil
            }
        }
        return folderURL
    }
}

当我调用此调用时,它给了我错误

您无权将文件“FolderName”保存在“SelectedFolder”文件夹中。:/Users/USERNAME/Workspace/SelectedFolder/FolderName

我看过一个类似的帖子并尝试了所有方法,但它仍然给我错误,我在这里遗漏了什么吗?任何帮助表示赞赏

4

1 回答 1

4

我假设您的应用程序是沙盒的。因此,您无权为您尝试的位置写入文件夹。

如果它不适合沙盒,您可以禁用应用沙盒,可以通过单击您的项目文件 > 目标名称、选择功能选项卡并关闭应用沙盒来关闭它。

文件系统编程指南:https ://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

此处的应用沙盒文档:https ://developer.apple.com/library/archive/documentation/Security/Conceptual/AppSandboxDesignGuide/AppSandboxInDepth/AppSandboxInDepth.html

您还可以查看安全范围的书签以进行持久资源访问。

此处的文档:

https://developer.apple.com/library/archive/documentation/Security/Conceptual/AppSandboxDesignGuide/AppSandboxInDepth/AppSandboxInDepth.html#//apple_ref/doc/uid/TP40011183-CH3-SW16

https://developer.apple.com/library/archive/documentation/Miscellaneous/Reference/EntitlementKeyReference/Chapters/EnablingAppSandbox.html#//apple_ref/doc/uid/TP40011195-CH4-SW18

于 2018-10-10T13:51:41.070 回答