【发布时间】:2018-06-22 08:19:25
【问题描述】:
尝试在应用程序支持中创建嵌套目录,但失败了。我收到的错误消息是"You don’t have permission to save the file 'folder1' in the folder 'testApp'。
let path = getApplicationSupportDirectory()
let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as! String
let folder = path.appendingPathComponent("\(appName)/folder1", isDirectory: true)
print("[ERR]: Folder location: \(folder.relativePath)")
if !FileManager.default.fileExists(atPath: folder.relativePath) {
do {
try FileManager.default.createDirectory(atPath: folder.relativeString, withIntermediateDirectories: true, attributes: nil)
} catch {
print("[ERR]: \(error.localizedDescription)")
}
}
文件夹位置输出正确的位置,它似乎是在“appName”中创建第一个目录。
【问题讨论】:
-
getApplicationSupportDirectory()返回的是什么?尝试用FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last!替换它?您需要对尝试写入的文件夹具有写入权限... -
@Michael - getApplicationSupportDirectory() 返回应用程序支持目录的绝对 URL。如何对新文件夹启用写权限?
-
基本上不使用
relativePath / relativeString作为文件系统URL。只需使用path属性。除了fileExists,建议始终使用与 URL 相关的 API。 -
@vadian - 更改为
path仍然是问题。 -
为什么投反对票?这个问题是完全有效的,并且与嵌套目录的创建有关。
标签: ios swift file-manager