[swift] Swift로 파일 이동 시 이동된 파일의 수정일자 업데이트 방법
파일을 이동할 때, 파일의 수정일자를 유지하거나 업데이트하는 방법은 Swift
에서 기본적으로 제공되지 않습니다. 하지만, FileManager
클래스를 사용하여 이를 구현할 수 있습니다.
Step 1: 파일 이동
먼저, FileManager
클래스의 moveItem
메서드를 사용하여 파일을 이동합니다.
let fileManager = FileManager.default
let sourceURL = URL(fileURLWithPath: "이동 전 파일 경로")
let destinationURL = URL(fileURLWithPath: "이동 후 파일 경로")
do {
try fileManager.moveItem(at: sourceURL, to: destinationURL)
} catch {
print("파일을 이동하는 중에 오류가 발생했습니다: \(error)")
}
Step 2: 수정일자 업데이트
파일을 이동한 후, 파일의 수정일자를 업데이트하려면 setAttributes
메서드를 사용하여 새로운 수정일자를 설정해야 합니다.
do {
let attributes = [FileAttributeKey.modificationDate: Date()]
try fileManager.setAttributes(attributes, ofItemAtPath: destinationURL.path)
} catch {
print("수정일자를 업데이트하는 중에 오류가 발생했습니다: \(error)")
}
결론
위의 단계를 따라 하면 파일을 이동한 후에도 수정일자를 업데이트할 수 있습니다. 이렇게 하면 파일을 이동할 때 수정일자 정보를 보존하거나 업데이트할 수 있습니다.
이러한 방법을 사용하여 Swift
로 파일 이동 시 수정일자를 업데이트하는 방법을 참고하세요.
참고 자료
- Swift FileManager - Apple Developer Documentation
- Swift FileAttributeKey - Apple Developer Documentation