[swift] Swift에서 생성된 디렉토리에 하위 디렉토리 추가하기
Swift 프로그램에서 디렉토리를 다루는 것은 중요한 작업 중 하나입니다. 특히, 기존 디렉토리에 새로운 하위 디렉토리를 추가해야 하는 경우가 있습니다. 이를 위해 Foundation 프레임워크에서 제공하는 FileManager 클래스를 사용할 수 있습니다.
아래는 Swift에서 생성된 디렉토리에 새로운 하위 디렉토리를 추가하는 방법을 보여주는 예제 코드입니다.
import Foundation
// 기존 디렉토리 경로
let directoryPath = "/path/to/your/directory"
// 추가할 하위 디렉토리 이름
let subDirectoryName = "newSubDirectory"
let fileManager = FileManager.default
let newDirectoryPath = directoryPath + "/" + subDirectoryName
// 디렉토리 추가
do {
try fileManager.createDirectory(atPath: newDirectoryPath, withIntermediateDirectories: true, attributes: nil)
print("하위 디렉토리가 성공적으로 추가되었습니다.")
} catch {
print("하위 디렉토리 추가 실패: \(error)")
}
위의 예제 코드는 FileManager
클래스를 사용하여 createDirectory
메서드를 호출하여 새로운 하위 디렉토리를 추가하는 방법을 보여줍니다. 이때, withIntermediateDirectories
매개변수를 true
로 설정하면 필요한 중간 디렉토리도 만들어집니다.
이렇게 하면 Swift에서 생성된 디렉토리에 새로운 하위 디렉토리를 성공적으로 추가할 수 있습니다.
더 많은 정보를 원하시면 Apple 개발자 문서를 참조하시기 바랍니다.