let filename = getDocumentsDirectory().appendingPathComponent(upload.fileName)
print("deleting")
let fileNameToDelete = upload.fileName
var filePath = ""
// Fine documents directory on device
let dirs : [String] = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true)
if dirs.count > 0 {
let dir = dirs[0] //documents directory
filePath = dir.appendingFormat("/" + fileNameToDelete)
print("Local path = \(filePath)")
} else {
print("Could not find local directory to store file")
return
}
print(filename)
print("deleting111111")
do {
let fileManager = FileManager.default
// Check if file exists
print("filePath")
print(filePath)
print("filePath")
if fileManager.fileExists(atPath: filePath) {
// Delete file
try fileManager.removeItem(atPath: filePath)
} else {
print("File does not exist")
}
}
catch let error as NSError {
print("An error took place: \(error)")
}}
これは以下のとおりです。なぜ削除が機能しないのですか?ファイルが存在する上で私に投げるの機能を超えているなぜあなたはスウィフト削除イメージファイル、スローファイルが存在しません
filename = getDocumentsDirectory().appendingPathComponent(nameOfImage+".PNG")
で生成されたファイル名に書き込むことで、ファイルを作成した場合、それは...それはある
deleting
Local path = /var/mobile/Containers/Data/Application/C763B3ED-3371-47AB-8F61-4F086D01E430/Documents/profile-FFCEBEA9-2F8D-49E2-9A09-2BF87BD0B542--A9636AF4-350D-4D72-A4BD-E4F2B183F4BB.png
file:///var/mobile/Containers/Data/Application/C763B3ED-3371-47AB-8F61-4F086D01E430/Documents/profile-FFCEBEA9-2F8D-49E2-9A09-2BF87BD0B542--A9636AF4-350D-4D72-A4BD-E4F2B183F4BB.png
deleting111111
filePath
/var/mobile/Containers/Data/Application/C763B3ED-3371-47AB-8F61-4F086D01E430/Documents/profile-FFCEBEA9-2F8D-49E2-9A09-2BF87BD0B542--A9636AF4-350D-4D72-A4BD-E4F2B183F4BB.png
filePath
File does not exist
ファイルが存在しますか? – vadian
これは以下のように使用して保存する必要があります。 –
func getDocumentsDirectory() - > URL { let paths = FileManager.default.urls(.documentDirectoryの場合:.userDomainMask) let documentsDirectory = paths [0] getDocumentsDirectory()。appendingPathComponent(nameOfImage + "PNG")は リターンdocumentsDirectory } –