In the previous Article, I wrote about how to add video, audio, etc. to the terminal using File Manager. Here's how to remove them.
Domain=NSCocoaErrorDomain Code=4 "” couldn’t be removed." UserInfo={NSUserStringVariant=( Remove ), NSFilePath=/var/mobile/Containers/Data/Application/EB92E676-C1F0-4B9A-8D82-D86D7186B2F3/Documents/dfg, NSUnderlyingError=0x2814ded00 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
iOS App
├── Documents --- test.txt //Delete files and folders added here
├── Library
│ ├── Caches
│ └── Preferences
└── tmp
func removeItem(_ itemName: String) {
let fileManager = FileManager.default
var pathString = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
if !fileManager.fileExists(atPath: pathString + "/" + itemName) {
print("The specified file or folder does not exist")
return
}
pathString = "file://" + pathString + "/" + itemName
guard let path = URL(string: pathString) else { return }
do {
try fileManager.removeItem(at: path)
print("Successful")
} catch let error {
print("failed\(error)")
}
}
Prepare the path to Documents with pathString
and use fileExists
to check if the folder or file entered as an argument after Documents exists. (The error when the specified item does not exist will be picked up on the 13th line, so it is not necessary here.)
Then change the path, convert it to a URL type and remove it using removeItem
.
Recommended Posts