[swift] 파일 이동하기

Swift 프로그램에서 파일을 이동하는 방법을 알아보겠습니다.

파일을 이동하기 위해서는 FileManager 클래스를 사용합니다. 다음은 파일을 이동하는 예제 코드입니다.

import Foundation

let fileManager = FileManager.default
let sourceURL = URL(fileURLWithPath: "/path/to/source/file.txt")
let destinationURL = URL(fileURLWithPath: "/path/to/destination/file.txt")

do {
    try fileManager.moveItem(at: sourceURL, to: destinationURL)
    print("파일 이동 성공")
} catch {
    print("파일 이동 실패: \(error)")
}

위 코드에서 sourceURL은 원본 파일 경로를, destinationURL은 이동할 파일 경로를 나타냅니다. moveItem(at:to:) 함수를 사용하여 파일을 이동할 수 있습니다.

파일 이동이 성공할 경우 “파일 이동 성공”이 출력되고, 실패할 경우에는 “파일 이동 실패”와 에러 메시지가 출력됩니다.

이 외에도 FileManager 클래스에는 다양한 파일 관리 메서드가 포함되어 있으니 필요한 경우에는 공식 문서나 다른 참고 자료를 참고하시기 바랍니다.

참고 자료: