I'm in trouble with file deletion in Swift Here is the problem, when I list the directory content I see all files When I select the one to delete with some condition, when I try to delete That throw me an error which says that files doesn't exist...
Here is the function
static func deleteAllVideosDownloaded() {
let fileManager = NSFileManager.defaultManager()
let directoryURL = fileManager.URLsForDirectory(.CachesDirectory, inDomains: .UserDomainMask)[0]
let enumerator: NSDirectoryEnumerator = fileManager.enumeratorAtPath(directoryURL.path!)!
while let element = enumerator.nextObject() as? String {
let url = NSURL(string: element)
let pathExtention = url!.pathExtension
print("All Elements : \(element)")
print("URL : \(url)")
if pathExtention == GlobalConstants.videoExtension {
print("Element = \(element)")
let path = directoryURL.URLByAppendingPathComponent(element)
Log.i("File Path : \(path)")
do {
try fileManager.removeItemAtPath(path.absoluteString)
} catch let error as NSError {
Log.e("Error while deleting a file: \(error)")
}
}
}
FileUtils.listCachesDirectoryContent()
}
If anyone sees what is wrong it'll be awesome, cause I'm stuck with since 3 hours at least.