[swift] 파일을 다른 경로로 이동하는 Swift 함수
먼저 다음과 같은 함수를 사용하여 파일을 이동할 수 있습니다.
import Foundation
func moveFile(atPath originPath: String, toPath destinationPath: String) {
let fileManager = FileManager.default
do {
try fileManager.moveItem(atPath: originPath, toPath: destinationPath)
print("File moved successfully")
} catch let error as NSError {
print("Error while moving file: \(error.localizedDescription)")
}
}
위 함수는 moveFile
이라는 이름을 가지며, originPath
에 있는 파일을 destinationPath
로 이동시킵니다. 에러가 발생하지 않는다면 “File moved successfully”를 출력하고, 에러가 발생한다면 해당 내용을 출력합니다.
예를 들어, originPath
와 destinationPath
는 파일의 전체 경로를 지정하는 문자열이어야 합니다. 또한, 해당 경로에 있는 파일이 실제로 존재해야 합니다.
위 함수를 사용하여 파일을 다른 경로로 이동할 수 있습니다.