I have a bug when my app runs on the iPhone but not when it runs on the simulator. I was using the length of the home directory path to extract the relative path of a file
A function (Swift 5) to remove an file from cache. I had a lot of problems and the use of the URL solved the /private issue.
class func removeFileFromCache( filePattern: String ) -> Bool {
// find filename with pattern
do {
let cachePath = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
let filePathUrl = URL( fileURLWithPath: filePattern )
let originalFilename = filePathUrl.lastPathComponent
let newFilePath = cachePath.appendingPathComponent( originalFilename ).path
try FileManager.default.removeItem(atPath: newFilePath )
print("file \( filePattern ) succesfull removed.",classname:URL(fileURLWithPath: #file).lastPathComponent )
} catch let error as NSError {
print("Error \(error)",classname:URL(fileURLWithPath: #file).lastPathComponent )
return false // all is not well
}
return true
}