[swift] Swift으로 디렉토리 생성 후 파일 생성 날짜 확인하기
iOS 또는 macOS 애플리케이션을 개발할 때, Swift를 사용하여 디렉토리를 생성하고 파일의 생성 날짜를 확인해야 하는 경우가 있습니다. Swift에서는 FileManager
클래스를 사용하여 이러한 작업을 수행할 수 있습니다. 이 기술 블로그에서는 Swift를 사용하여 디렉토리를 생성하고 파일의 생성 날짜를 확인하는 방법을 설명합니다.
디렉토리 생성
디렉토리를 생성하려면 다음과 같이 FileManager
클래스를 사용합니다.
let fileManager = FileManager.default
let documentsUrl = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
let newFolderUrl = documentsUrl.appendingPathComponent("NewFolder")
do {
try fileManager.createDirectory(at: newFolderUrl, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error)
}
위 코드에서는 FileManager
의 createDirectory
메서드를 사용하여 새로운 폴더를 생성합니다. createDirectory
메서드의 at
매개 변수에는 새로운 디렉토리의 URL을 지정합니다.
파일의 생성 날짜 확인
파일의 생성 날짜를 확인하려면 다음과 같이 URLResourceValues
를 사용하여 파일의 속성을 가져옵니다.
do {
let fileUrl = newFolderUrl.appendingPathComponent("example.txt")
let attributes = try fileManager.attributesOfItem(atPath: fileUrl.path)
if let creationDate = attributes[.creationDate] as? Date {
print("파일 생성 날짜: \(creationDate)")
}
} catch {
print(error)
}
위 코드에서는 URLResourceValues
의 creationDate
속성을 사용하여 파일의 생성 날짜를 확인합니다.
이제 Swift를 사용하여 디렉토리를 생성하고 파일의 생성 날짜를 확인하는 방법에 대해 알아보았습니다.
참고 문헌: Apple Developer Documentation - FileManager