[swift] Swift에서 파일 삭제 후에 디렉토리를 확인하는 방법

먼저, 필요한 Foundation 모듈을 import합니다.

import Foundation

다음으로, FileManager를 사용하여 파일을 삭제합니다.

let fileURL = URL(fileURLWithPath: "path/to/your/file.txt")

do {
    try FileManager.default.removeItem(at: fileURL)
} catch {
    print("Error: \(error)")
}

파일을 삭제한 후, 디렉토리를 확인하려면 다음과 같이 코드를 작성할 수 있습니다.

let directoryURL = URL(fileURLWithPath: "path/to/your/directory")

do {
    let fileURLs = try FileManager.default.contentsOfDirectory(at: directoryURL, includingPropertiesForKeys: nil)
    for url in fileURLs {
        print(url.path)
    }
} catch {
    print("Error: \(error)")
}

위의 코드는 지정된 디렉토리 내의 파일 목록을 가져와 출력하는 예시입니다.

참고 문헌: