ฉันกำลังพยายามคัดลอกหรือย้าย JPG จากโฟลเดอร์หนึ่งไปยังอีกโฟลเดอร์หนึ่งโดยใช้โค้ดด้านล่าง
func moveItems(originPath: String, destinationPath: String, successBlock:@escaping () -> Void, errorBlock:@escaping (_ error: Error) -> Void) {
do {
try FileManager.default.copyItem(atPath: originPath, toPath: destinationPath)
successBlock()
} catch {
print(error.localizedDescription)
errorBlock(NSError())
}
}
แต่ฉันมักจะได้รับข้อผิดพลาดต่อไปนี้:
เปิดบน /var/mobile/Containers/Data/Application/E138FDE0-A036-48D9-8E7C-518B6A5E452C/Documents/tailsImages/: มีไฟล์อยู่ “3A75A51A-0946-45AF-BF1D-F4EB4D71ED68.png” ไม่สามารถคัดลอกไปที่ “ เอกสาร” เนื่องจากมีรายการที่มีชื่อเดียวกันอยู่แล้ว
มีสองปัญหาที่ฉันไม่เข้าใจ สาเหตุหนึ่งคือโฟลเดอร์ปลายทางไม่ใช่ "เอกสาร" ที่ข้อผิดพลาดอ้างถึง และอีกประการหนึ่งคือ JPG มีอยู่ในโฟลเดอร์ DestinationPath เท่านั้น
(lldb) po originPath "/var/mobile/Containers/Data/Application/E138FDE0-A036-48D9-8E7C-518B6A5E452C/Documents/tailsImagesSync/3A75A51A-0946-45AF-BF1D-F4EB4D71ED68.png"
(lldb) po DestinationPath "/var/mobile/Containers/Data/Application/E138FDE0-A036-48D9-8E7C-518B6A5E452C/Documents/tailsImages/"