[swift] Swift로 파일 이동 시 목적지 위치에서 파일 복사하기

Swift에서 파일을 이동하고자 할 때 목적지 위치에 파일을 복사해야 할 때가 있습니다. 이때 FileManager 클래스를 사용하여 파일을 복사할 수 있습니다.

import Foundation

func copyFileToDestination(sourceURL: URL, destinationURL: URL) {
    let fileManager = FileManager.default
    do {
        try fileManager.copyItem(at: sourceURL, to: destinationURL)
    } catch {
        print("Error: \(error)")
    }
}

let sourceURL = URL(fileURLWithPath: "path_to_source_file")
let destinationURL = URL(fileURLWithPath: "path_to_destination_folder/destination_file_name")
copyFileToDestination(sourceURL: sourceURL, destinationURL: destinationURL)

위의 예제는 FileManager 클래스를 사용하여 파일을 복사하는 방법을 보여줍니다. copyItem(at:to:) 메서드를 이용하여 파일을 복사할 수 있으며, 이때 오류가 발생하면 catch 블록에서 오류를 처리할 수 있습니다.

이렇게 Swift에서 파일을 목적지 위치로 복사하는 방법을 사용할 수 있습니다.

관련 참고 자료:

이렇게 하면 파일 이동 시 목적지 위치에서 파일을 Swift로 복사할 수 있습니다.