从文档目录内的目录中删除文件?

我创build了一个Temp目录来存储一些文件:

//MARK: -create save delete from directory func createTempDirectoryToStoreFile(){ var error: NSError? let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) let documentsDirectory: AnyObject = paths[0] tempPath = documentsDirectory.stringByAppendingPathComponent("Temp") if (!NSFileManager.defaultManager().fileExistsAtPath(tempPath!)) { NSFileManager.defaultManager() .createDirectoryAtPath(tempPath!, withIntermediateDirectories: false, attributes: nil, error: &error) } } 

这很好,现在我想删除目录中的所有文件…我试着如下:

 func clearAllFilesFromTempDirectory(){ var error: NSErrorPointer = nil let dirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String var tempDirPath = dirPath.stringByAppendingPathComponent("Temp") var directoryContents: NSArray = fileManager.contentsOfDirectoryAtPath(tempDirPath, error: error)! if error == nil { for path in directoryContents { let fullPath = dirPath.stringByAppendingPathComponent(path as! String) let removeSuccess = fileManager.removeItemAtPath(fullPath, error: nil) } }else{ println("seomthing went worng \(error)") } } 

我注意到,文件仍然存在…我做错了什么?

有两件事,使用temp目录,然后传递一个错误到fileManager.removeItemAtPath ,并将其放置在一个if来查看失败。 你也不应该检查错误是否被设置,而是一个方法是否有返回数据。

 func clearAllFilesFromTempDirectory(){ var error: NSErrorPointer = nil let dirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String var tempDirPath = dirPath.stringByAppendingPathComponent("Temp") var directoryContents: NSArray = fileManager.contentsOfDirectoryAtPath(tempDirPath, error: error)? if directoryContents != nil { for path in directoryContents { let fullPath = dirPath.stringByAppendingPathComponent(path as! String) if fileManager.removeItemAtPath(fullPath, error: error) == false { println("Could not delete file: \(error)") } } } else { println("Could not retrieve directory: \(error)") } } 

要获得正确的临时目录,请使用NSTemporaryDirectory()

如果任何人需要这个最新的Swift / Xcode版本:这里是一个从临时文件夹中删除所有文件的例子:

Swift 2.x:

 func clearTempFolder() { let fileManager = NSFileManager.defaultManager() let tempFolderPath = NSTemporaryDirectory() do { let filePaths = try fileManager.contentsOfDirectoryAtPath(tempFolderPath) for filePath in filePaths { try fileManager.removeItemAtPath(tempFolderPath + filePath) } } catch { print("Could not clear temp folder: \(error)") } } 

Swift 3.xSwift 4:

 func clearTempFolder() { let fileManager = FileManager.default let tempFolderPath = NSTemporaryDirectory() do { let filePaths = try fileManager.contentsOfDirectory(atPath: tempFolderPath) for filePath in filePaths { try fileManager.removeItem(atPath: tempFolderPath + filePath) } } catch { print("Could not clear temp folder: \(error)") } } 

Swift 3

  func clearTempFolder() { let fileManager = FileManager.default let tempFolderPath = NSTemporaryDirectory() do { let filePaths = try fileManager.contentsOfDirectory(atPath: tempFolderPath) for filePath in filePaths { try fileManager.removeItem(atPath: NSTemporaryDirectory() + filePath) } } catch let error as NSError { print("Could not clear temp folder: \(error.debugDescription)") } }