ios - 快速删除图像文件,抛出文件不存在

标签 ios swift swift3 nsfilemanager

 let filename = getDocumentsDirectory().appendingPathComponent(upload.fileName)
               print("deleting")
            let fileNameToDelete = upload.fileName
            var filePath = ""          
            // Fine documents directory on device
            let dirs : [String] = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true)          
            if dirs.count > 0 {
                let dir = dirs[0] //documents directory
                filePath = dir.appendingFormat("/" + fileNameToDelete)
                print("Local path = \(filePath)")      
            } else {
                print("Could not find local directory to store file")
                return
            }         
            print(filename)           
              print("deleting111111")
            do {
                let fileManager = FileManager.default  
                // Check if file exists
                print("filePath")
                print(filePath)
                 print("filePath")
                if fileManager.fileExists(atPath: filePath) {
                    // Delete file
                    try fileManager.removeItem(atPath: filePath)
                } else {
                    print("File does not exist")
                }            
            }
            catch let error as NSError {
                print("An error took place: \(error)")
            }}

这会打印在下面。为什么删除不起作用?为什么上面的函数向我抛出文件存在但它不存在

deleting
Local path = /var/mobile/Containers/Data/Application/C763B3ED-3371-47AB-8F61-4F086D01E430/Documents/profile-FFCEBEA9-2F8D-49E2-9A09-2BF87BD0B542--A9636AF4-350D-4D72-A4BD-E4F2B183F4BB.png
file:///var/mobile/Containers/Data/Application/C763B3ED-3371-47AB-8F61-4F086D01E430/Documents/profile-FFCEBEA9-2F8D-49E2-9A09-2BF87BD0B542--A9636AF4-350D-4D72-A4BD-E4F2B183F4BB.png
deleting111111
filePath
/var/mobile/Containers/Data/Application/C763B3ED-3371-47AB-8F61-4F086D01E430/Documents/profile-FFCEBEA9-2F8D-49E2-9A09-2BF87BD0B542--A9636AF4-350D-4D72-A4BD-E4F2B183F4BB.png
filePath
File does not exist

最佳答案

如果您通过写入生成的文件名来创建文件

filename = getDocumentsDirectory().appendingPathComponent(nameOfImage+"‌​.PNG")

...那么绝对重要是,当需要删除文件时,您以完全相同的方式生成文件名。那不是你正在做的事情。

事实上,在您显示的代码中,您确实生成了一个名为 filename 的变量,其代码看起来类似:

let filename = getDocumentsDirectory().appendingPathComponent(upload.fileName)

...但是你永远不会使用 文件名做任何事情!因此你不断地伪装自己。您创建文件名,打印文件名,但不使用文件名作为要删除的路径。您使用其他一些变量,filePath,以不同的方式获得。

关于ios - 快速删除图像文件,抛出文件不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42043121/

相关文章:

ios - 应用程序在 textview.becomeFirstResponder 处崩溃

ios - 用于配置具有多个 Firebase 调用的 View 的数据源

ios - Xcode 中未显示 Material Design 文本字段自定义类

swift - 我如何通过子字符串获取点后的数字?

swift - 从 Firebase 中删除所有用户帖子时无法识别的选择器

ios - NSStreams-code 仅在调试器模式下按预期工作

swift - 更新到 Swift 3 时出现明确指定的类型 'NSURL?' 问题

ios - Apple Private SDK 的合法性(应用商店除外)

ios - 如何查看iOS11录屏是开还是关?

objective-c - UINavigationController 和 UIViewController 是如何工作的?