[swift] Swift 디렉토리 삭제 기능의 기본 개념
이번 포스트에서는 Swift에서 디렉토리 삭제 기능을 구현하는 방법에 대해 알아보겠습니다.
디렉토리 삭제 함수
Swift에서 디렉토리를 삭제하기 위해서는 FileManager
클래스의 removeItem(atPath:)
메서드를 사용합니다. 이 메서드를 사용하면 파일 또는 디렉토리를 삭제할 수 있습니다.
import Foundation
func deleteDirectory(atPath path: String) {
let fileManager = FileManager.default
do {
try fileManager.removeItem(atPath: path)
print("디렉토리 삭제 완료")
} catch {
print("디렉토리 삭제 실패: \(error)")
}
}
// 사용 예시
let directoryPath = "/path/to/directory"
deleteDirectory(atPath: directoryPath)
위의 예제 코드에서는 deleteDirectory
함수를 사용하여 지정된 경로에 있는 디렉토리를 삭제합니다.
요약
Swift에서 디렉토리를 삭제하는 방법은 FileManager
클래스의 removeItem(atPath:)
메서드를 사용하는 것입니다. 해당 메서드를 사용하여 지정된 경로에 있는 디렉토리를 삭제할 수 있습니다.
이상으로 Swift에서 디렉토리 삭제 기능에 대한 기본적인 개념을 학습해 보았습니다.