I'm trying to remove a file in the Documents directory, but it says that the file doesn't exist! Here's my coding:
let zipFiles = documentSTRINGS.filter{$0.hasSuffix("zip")}
....
....
do{
print (zipFiles)
try FileManager.default.removeItem(atPath: zipFiles.first!)
}
catch{
print (error)
}zipFiles prints ["file:///private/var/mobile/Containers/Data/Application/XXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXX/Documents/Accoltus.zip"]. So I know the file exists, but it's printing the following error:
Error Domain=NSCocoaErrorDomain Code=4 "“Accoltus.zip” couldn’t be removed." UserInfo={NSFilePath=file:/
Remove
), NSUnderlyingError=0x1c445d730 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
This isn't making any sense. Does anyone have any clue why this is happening?
No, your zipFiles array consists of a URL string (it has the "file:" scheme at the start of it), so it's not a file path.
Whatever is producing your documentSTRINGS array is putting the wrong thing in that array.
P.S. In general, do not use path-based APIs like "removeItem(atPath:)". Use URL-based APIs instead, such as "removeItem(at:)".