How do I delete a file in my apps documents directory?
Given, a file of path [[self documentsDirectory] stringByAppendingPathComponent:@"my开发者_JAVA百科Files/aFile.txt"]
, How do I delete a file in my apps documents directory?
NSFileManager is a very handy tool:
[[NSFileManager defaultManager] removeItemAtPath: pathToFile error: &error];
With Swift 3, FileManager
has a method called removeItem(at:)
. removeItem(at:)
has the following declaration:
func removeItem(at URL: URL) throws
Removes the file or directory at the specified URL.
The code fragment below shows how to delete a file using removeItem(at:)
:
do {
try FileManager.default.removeItem(at: fileUrl)
} catch {
print("Could not delete file: \(error)")
}
As a complete example, the following UIViewController
subclass implementation shows how to copy a file from the app bundle to a specific url in the document directory, delete a file at a specific url in document directory and check if a file exists at a specific url in document directory:
import UIKit
class ViewController: UIViewController {
@IBAction func copyFile(_ sender: UIButton) {
// Get app bundle file url
guard let bundleFileUrl = Bundle.main.url(forResource: "Movie", withExtension: "mov") else { return }
// Create a destination url in document directory for file
guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
let documentDirectoryFileUrl = documentsDirectory.appendingPathComponent("Movie.mov")
// Copy bundle file to document directory
if !FileManager.default.fileExists(atPath: documentDirectoryFileUrl.path) {
do {
try FileManager.default.copyItem(at: bundleFileUrl, to: documentDirectoryFileUrl)
} catch {
print("Could not copy file: \(error)")
}
}
}
@IBAction func deleteFile(_ sender: UIButton) {
// Get destination url in document directory for file
guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
let documentDirectoryFileUrl = documentsDirectory.appendingPathComponent("Movie.mov")
// Delete file in document directory
if FileManager.default.fileExists(atPath: documentDirectoryFileUrl.path) {
do {
try FileManager.default.removeItem(at: documentDirectoryFileUrl)
} catch {
print("Could not delete file: \(error)")
}
}
}
@IBAction func checkIfFileExist(_ sender: UIButton) {
// Get destination url in document directory for file
guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
let outputUrl = documentsDirectory.appendingPathComponent("Movie.mov")
// Print if file exists in document directory or not
FileManager.default.fileExists(atPath: outputUrl.path) ? print("File exists") : print("File does not exist")
}
}
For Swift 3.x and Xcode 8.x
func removeItemForDocument(itemName:String, fileExtension: String) {
let fileManager = FileManager.default
let NSDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
let NSUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
let paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true)
guard let dirPath = paths.first else {
return
}
let filePath = "\(dirPath)/\(itemName).\(fileExtension)"
do {
try fileManager.removeItem(atPath: filePath)
} catch let error as NSError {
print(error.debugDescription)
}}
精彩评论