[swift] Swift로 생성된 디렉토리에 실행 권한 설정하기
Swift에서 디렉토리에 실행 권한을 설정하려면 FileManager
클래스를 사용하여 파일 시스템 작업을 수행해야 합니다. 먼저, FileManager.default
를 사용하여 기본 파일 관리자를 가져올 수 있습니다.
다음으로, setAttributes(_:ofItemAtPath:)
메서드를 사용하여 디렉토리의 속성을 설정합니다. 이때, FileAttributeKey
구조체를 사용하여 실행 권한을 포함한 디렉토리의 속성을 정의합니다.
아래는 Swift 코드 예제입니다.
import Foundation
let fileManager = FileManager.default
let directoryPath = "/path/to/directory"
do {
var attributes = try fileManager.attributesOfItem(atPath: directoryPath)
attributes[.posixPermissions] = NSNumber(value: Int16(0o755)) // 0755 permission
try fileManager.setAttributes(attributes, ofItemAtPath: directoryPath)
print("Execution permission set for directory: \(directoryPath)")
} catch {
print("Error: \(error.localizedDescription)")
}
위의 코드는 FileManager
클래스의 attributesOfItem(atPath:)
메서드를 사용하여 디렉토리의 현재 속성을 가져 옵니다. 그런 다음, FileAttributeKey.posixPermissions
를 사용하여 실행 권한을 755(모든 사용자에 대한 읽기 및 실행 권한)로 설정합니다. 마지막으로, setAttributes(_:ofItemAtPath:)
메서드를 사용하여 디렉토리의 새로운 속성을 적용합니다.
이렇게 하면 Swift를 사용하여 디렉토리에 실행 권한을 설정할 수 있습니다.