我在我的应用程序中下载了一些PDF文件,并希望在关闭应用程序时删除这些文件.
由于某种原因,它不起作用:
创建文件:
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")
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我想调用该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)")
}
}
Run Code Online (Sandbox Code Playgroud)
Sal*_*sum 46
这段代码适合我.我删除了所有缓存的图像.
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)")
}
}
Run Code Online (Sandbox Code Playgroud)
****更新swift 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)")
}
Run Code Online (Sandbox Code Playgroud)
Two*_*aws 19
我相信你的问题就在这条线上:
let filePaths = try fileManager.contentsOfDirectoryAtPath("\(documentsUrl)")
Run Code Online (Sandbox Code Playgroud)
你正在使用contentsOfDirectoryAtPath()的东西是一个NSURL.您可以选择路径字符串或URL,而不是尝试将它们混合使用.要预先清空您可能的下一个问题,首选网址.尝试使用contentsOfDirectoryAtURL()和removeItemAtURL().
解决上述问题后,您应该注意另一个奇怪的事情:NSTemporaryDirectory()当您尝试删除时,为什么要使用文件路径?您正在阅读文档目录并应该使用它.
斯威夫特5:
// start with a file path, for example:
let fileUrl = FileManager.default.urls(
for: .documentDirectory,
in: .userDomainMask
).deletingPathExtension()
.appendingPathComponent(
"someDir/customFile.txt",
isDirectory: false
)
// check if file exists
// fileUrl.path converts file path object to String by stripping out `file://`
if FileManager.default.fileExists(atPath: fileUrl.path) {
// delete file
do {
try FileManager.default.removeItem(atPath: fileUrl.path)
} catch {
print("Could not delete file, probably read-only filesystem")
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
35485 次 |
| 最近记录: |