我使用以下方法在文档目录中创建了一个文件夹:
fileManager.createDirectory(atPath:ziPFolderPath,withIntermediateDirectories: false, attributes: nil)
在这个文件夹中我放了几个文件
稍后在应用程序中,我不仅要删除上面文件夹中的文件,还要删除文件夹
FileManager
支持removeItem
功能,但我想知道它是否也删除了该文件夹。
答案 0 :(得分:7)
是的,它也会删除文件夹。
来自以下文档: - removeItem(at:)
删除指定网址的文件或目录。
来自以下文档: - removeItem(atPath:)
删除指定路径的文件或目录。
编辑:您可以这样调用它。
try? FileManager.default.removeItem(at: URL(fileURLWithPath: ziPFolderPath))
//OR
try? FileManager.default.removeItem(atPath: ziPFolderPath)
答案 1 :(得分:0)
-(BOOL)removeItemAtPath:(NSString *)path
error:(NSError * _Nullable *)error;
path是指示要删除的目录或文件夹的字符串。它是一个NSFileManager方法。
答案 2 :(得分:0)
快速5
此外,您还应该检查文件是否存在于路径中,并检查是否存在错误。
do {
let fileManager = FileManager.default
// Check if file exists
if fileManager.fileExists(atPath: urlfilePath) {
// Delete file
try fileManager.removeItem(atPath: urlfilePath)
} else {
print("File does not exist")
}
}
catch let error as NSError {
print("An error took place: \(error)")
}
}