[swift] Swift에서 저장된 파일을 다른 위치로 이동하는 방법
파일을 다른 위치로 이동하는 것은 Swift에서 매우 간단합니다. FileManager
클래스를 사용하여 현재 파일의 위치를 확인하고 이동할 위치에 새 파일 경로로 해당 파일을 이동시키면 됩니다.
아래에는 기본적인 예제 코드가 있습니다.
import Foundation
let fileManager = FileManager.default
let currentPath = "현재 파일 경로"
let destinationPath = "이동할 경로"
do {
try fileManager.moveItem(atPath: currentPath, toPath: destinationPath)
print("파일을 성공적으로 이동했습니다.")
} catch let error as NSError {
print("파일 이동 중 오류 발생: \(error)")
}
위의 코드에서 FileManager
의 moveItem(atPath:toPath:)
메서드를 사용하여 파일을 이동할 수 있습니다. 만약 파일을 성공적으로 이동하지 못하면 오류가 발생할 것이므로, do-catch
구문을 사용하여 오류를 처리할 수 있습니다.