Delete files from directory inside Document directory?

前端 未结 8 864
不思量自难忘°
不思量自难忘° 2020-12-24 01:48

I have created a Temp directory to store some files:

//MARK: -create save delete from directory
func createTempDirectoryToStoreFile(){
    var error: NSError         


        
8条回答
  •  没有蜡笔的小新
    2020-12-24 02:20

    Two things, use the temp directory and second pass an error to the fileManager.removeItemAtPath and place it in a if to see what failed. Also you should not be checking if the error is set but rather whether a methods has return data.

    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)")
        }
    }
    

    To get the correct temporary directory use NSTemporaryDirectory()

提交回复
热议问题