使用Swift删除iOS目录中的文件
我在我的应用程序中下载了一些PDF文件,并希望在closures应用程序时删除这些文件。
由于某种原因,它不起作用:
创build文件:
let reference = "test.pdf" let RequestURL = "http://xx/_PROJEKTE/xx\(self.reference)" let ChartURL = NSURL(string: RequestURL) //download file let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first! as NSURL let destinationUrl = documentsUrl.URLByAppendingPathComponent(ChartURL!.lastPathComponent!) if NSFileManager().fileExistsAtPath(destinationUrl.path!) { print("The file already exists at path") } else { // if the file doesn't exist // just download the data from your url if let ChartDataFromUrl = NSData(contentsOfURL: ChartURL!){ // after downloading your data you need to save it to your destination url if ChartDataFromUrl.writeToURL(destinationUrl, atomically: true) { print("file saved") print(destinationUrl) } else { print("error saving file") } } }
然后我想调用test()
函数来删除这些项目,如下所示:
func test(){ let fileManager = NSFileManager.defaultManager() let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first! as NSURL do { let filePaths = try fileManager.contentsOfDirectoryAtPath("\(documentsUrl)") for filePath in filePaths { try fileManager.removeItemAtPath(NSTemporaryDirectory() + filePath) } } catch { print("Could not clear temp folder: \(error)") } }
我相信你的问题在这一行:
let filePaths = try fileManager.contentsOfDirectoryAtPath("\(documentsUrl)")
你正在使用contentsOfDirectoryAtPath()
和NSURL
。 您selectpathstring或URL,而不是尝试将它们混合。 要预先清空您可能的下一个问题,首选url。 尝试使用contentsOfDirectoryAtURL()
和removeItemAtURL()
。
另一个好奇的事情,你应该看看,一旦你解决上述问题:当你尝试删除时,为什么你使用NSTemporaryDirectory()
作为文件path? 你正在阅读文档目录,应该使用它。
这段代码适用于我。 我删除了所有caching的图像。
private func test(){ let fileManager = NSFileManager.defaultManager() let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.CachesDirectory, inDomains: .UserDomainMask).first! as NSURL let documentsPath = documentsUrl.path do { if let documentPath = documentsPath { let fileNames = try fileManager.contentsOfDirectoryAtPath("\(documentPath)") print("all files in cache: \(fileNames)") for fileName in fileNames { if (fileName.hasSuffix(".png")) { let filePathName = "\(documentPath)/\(fileName)" try fileManager.removeItemAtPath(filePathName) } } let files = try fileManager.contentsOfDirectoryAtPath("\(documentPath)") print("all files in cache after deleting images: \(files)") } } catch { print("Could not clear temp folder: \(error)") } }
****更新迅速3 ****
let fileManager = FileManager.default let documentsUrl = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first! as NSURL let documentsPath = documentsUrl.path do { if let documentPath = documentsPath { let fileNames = try fileManager.contentsOfDirectory(atPath: "\(documentPath)") print("all files in cache: \(fileNames)") for fileName in fileNames { if (fileName.hasSuffix(".png")) { let filePathName = "\(documentPath)/\(fileName)" try fileManager.removeItem(atPath: filePathName) } } let files = try fileManager.contentsOfDirectory(atPath: "\(documentPath)") print("all files in cache after deleting images: \(files)") } } catch { print("Could not clear temp folder: \(error)") }