Swift - 从文档目录中删除自定义文件夹
Swift -Delete Custom Folder From Documents Directory
录制视频时,我使用 "/MyFolder"
创建自定义文件夹,如下所示:
guard let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first else { return }
let dirPath = "\(documentsPath)/MyFolder/Videos_\(UUID().uuidString).mp4"
let outputFileURL = URL(fileURLWithPath: dirPath)
// ...
现在我有一个功能可以只删除自定义文件夹:
func deleteCustom(folder: String) {
let fileManager = FileManager.default
guard let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
let customFolder = documentsDirectory.appendingPathComponent(folder)
guard let filePaths = try? fileManager.contentsOfDirectory(at: customFolder, includingPropertiesForKeys: nil, options: []) else { return }
for filePath in filePaths {
try? fileManager.removeItem(at: filePath)
}
}
在函数参数中应该传入"MyFolder"
还是"/MyFolder"
?
In the function parameter should I pass in "MyFolder" or "/MyFolder"?
"MyFolder",因为appendingPathComponent会自动添加/。
录制视频时,我使用 "/MyFolder"
创建自定义文件夹,如下所示:
guard let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first else { return }
let dirPath = "\(documentsPath)/MyFolder/Videos_\(UUID().uuidString).mp4"
let outputFileURL = URL(fileURLWithPath: dirPath)
// ...
现在我有一个功能可以只删除自定义文件夹:
func deleteCustom(folder: String) {
let fileManager = FileManager.default
guard let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
let customFolder = documentsDirectory.appendingPathComponent(folder)
guard let filePaths = try? fileManager.contentsOfDirectory(at: customFolder, includingPropertiesForKeys: nil, options: []) else { return }
for filePath in filePaths {
try? fileManager.removeItem(at: filePath)
}
}
在函数参数中应该传入"MyFolder"
还是"/MyFolder"
?
In the function parameter should I pass in "MyFolder" or "/MyFolder"?
"MyFolder",因为appendingPathComponent会自动添加/。