[swift] Swift로 파일 이동 시 이동 전 소스 파일에 대한 삭제 작업 추가

Swift로 파일을 이동할 때 이동 전 소스 파일을 삭제하는 작업을 추가하는 방법을 알아보겠습니다.

일반적으로 파일을 이동하고 나면 이동 전 파일을 삭제하는 것이 좋은 방법입니다. Swift에서 파일을 이동하고 삭제하는 방법은 다음과 같습니다.

첫째, FileManager 클래스를 사용하여 파일을 이동하고 추가적으로 삭제하는 작업을 수행합니다. 아래는 예시 코드입니다.

import Foundation

let fileManager = FileManager.default
let sourceURL = URL(fileURLWithPath: "path_to_source_file")
let destinationURL = URL(fileURLWithPath: "path_to_destination_file")

do {
    try fileManager.moveItem(at: sourceURL, to: destinationURL)
    try fileManager.removeItem(at: sourceURL) // 이동 전 파일 삭제
    print("File moved and source file deleted successfully.")
} catch {
    print("Error: \(error)")
}

위의 코드에서 moveItem(at:to:)를 사용하여 파일을 이동하고, removeItem(at:)를 사용하여 이동 전 파일을 삭제합니다.

이렇게 함으로써, Swift로 파일을 이동한 후 이동 전 파일을 삭제하는 작업을 추가할 수 있습니다.

이 글이 도움이 되었기를 바랍니다.

파일 관리 - Apple Developer 문서