在 Swift 中的应用程序支持中创建嵌套目录
Create nested directory in Application Support in Swift
尝试在应用程序支持中创建嵌套目录,但失败了。我收到的错误消息是 "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" 中创建了第一个目录。
请尝试此代码,它应该有效。
它使用 FileManager
的 API 创建 Application Support 文件夹(如果不存在)。
do {
let applicationSupportFolderURL = try FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as! String
let folder = applicationSupportFolderURL.appendingPathComponent("\(appName)/folder1", isDirectory: true)
print("[ERR]: Folder location: \(folder.path)")
if !FileManager.default.fileExists(atPath: folder.path) {
try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true, attributes: nil)
}
} catch { print(error) }
尝试在应用程序支持中创建嵌套目录,但失败了。我收到的错误消息是 "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" 中创建了第一个目录。
请尝试此代码,它应该有效。
它使用 FileManager
的 API 创建 Application Support 文件夹(如果不存在)。
do {
let applicationSupportFolderURL = try FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as! String
let folder = applicationSupportFolderURL.appendingPathComponent("\(appName)/folder1", isDirectory: true)
print("[ERR]: Folder location: \(folder.path)")
if !FileManager.default.fileExists(atPath: folder.path) {
try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true, attributes: nil)
}
} catch { print(error) }