[swift] Swift으로 현재 작업 디렉토리에 새로운 디렉토리 만들기

목차

  1. Swift으로 현재 작업 디렉토리 확인하기
  2. Swift으로 새로운 디렉토리 만들기

1. Swift으로 현재 작업 디렉토리 확인하기

먼저, 현재 작업 디렉토리를 확인하는 방법을 알아보겠습니다. Swift에서는 FileManager를 사용하여 현재 작업 디렉토리의 경로를 얻을 수 있습니다.

let fileManager = FileManager.default
let currentPath = fileManager.currentDirectoryPath
print("Current Directory: \(currentPath)")

위의 코드를 실행하면 현재 작업 디렉토리의 경로가 콘솔에 출력됩니다.

2. Swift으로 새로운 디렉토리 만들기

이제 새로운 디렉토리를 만들어보겠습니다. Swift에서는 FileManager를 사용하여 새로운 디렉토리를 만들 수 있습니다.

let newDirectoryName = "NewDirectory"
let newDirectoryPath = currentPath + "/" + newDirectoryName

do {
    try fileManager.createDirectory(atPath: newDirectoryPath, withIntermediateDirectories: true, attributes: nil)
    print("New directory created at: \(newDirectoryPath)")
} catch {
    print("Error creating directory: \(error.localizedDescription)")
}

위의 코드를 실행하면 현재 작업 디렉토리에 “NewDirectory”라는 새로운 디렉토리가 생성되고, 생성된 디렉토리의 경로가 콘솔에 출력됩니다.

이상으로 Swift에서 현재 작업 디렉토리에 새로운 디렉토리를 만드는 방법에 대해 알아보았습니다.

참고문헌: