Я пытаюсь скопировать файл (audioFile) в DestinationURl, однако, когда я получаю эту ошибку:
Unable to create directory Error Domain=NSCocoaErrorDomain Code=516 "“20200116183746+0000.m4a” couldn’t be copied to “Documents” because an item with the same name already exists." UserInfo={NSSourceFilePathErrorKey=/<app>/Documents/20200116183746+0000.m4a, NSUserStringVariant=(
Copy
), NSDestinationFilePath=<appURL>/Documents/20200116183746+0000.m4a, NSUnderlyingError=0x600003c2d290 {Error Domain=NSPOSIXErrorDomain Code=17 "File exists"}}
Вот я мой код
let DirPath = DocumentDirectory.appendingPathComponent(self.txtName.text ?? "NA")
do
{
try FileManager.default.createDirectory(atPath: DirPath!.path, withIntermediateDirectories: true, attributes: nil)
print("
Audio file: (self.audioFile)
")
let desitationURl = ("(DirPath!.path)/")
try FileManager.default.copyItem(atPath: self.audioFile.path, toPath: desitationURl)
}
catch let error as NSError
{
print("Unable to create directory (error.debugDescription)")
}
Я удалил / on let desitationURl = ("(DirPath! .Path) /") и вижу, что файл сгенерирован и папка сгенерирована, но ничего не перемещено.
Любая помощь будет оценена
Osian
Всего 1 ответ
Вам нужно указать фактическое имя файла назначения, а не только каталог, в котором он будет находиться. Файловый менеджер буквально пытается сохранить ваш файл как имя каталога. Я немного очистил ваш код, чтобы сделать его более читабельным:
guard let dirURL = DocumentDirectory
.appendingPathComponent(self.txtName.text ?? "NA") else { return }
if !FileManager.default.fileExists(atPath: dirURL.path) {
do {
try FileManager.default
.createDirectory(atPath: dirURL.path,
withIntermediateDirectories: true,
attributes: nil)
} catch let error as NSError {
print("Unable to create directory (error.debugDescription)")
}
}
print("
Audio file: (audioFile)
")
let dstURL = dirURL.appendingPathComponent(audioFile.lastPathComponent)
do {
try FileManager.default.copyItem(atPath: audioFile.path, toPath: dstURL.path)
} catch let error as NSError {
print("Unable to copy file (error.debugDescription)")
}