[swift] 파일을 특정 디렉토리에 저장하기
import Foundation

func saveFileToDirectory(fileUrl: URL, directoryPath: String) {
    let manager = FileManager.default
    let directoryUrl = URL(fileURLWithPath: directoryPath)
    
    // 디렉토리가 없는 경우 생성
    if !manager.fileExists(atPath: directoryPath) {
        do {
            try manager.createDirectory(at: directoryUrl, withIntermediateDirectories: true, attributes: nil)
        } catch {
            print(error.localizedDescription)
        }
    }
    
    let destinationUrl = directoryUrl.appendingPathComponent(fileUrl.lastPathComponent)
    
    do {
        try manager.copyItem(at: fileUrl, to: destinationUrl)
        print("파일이 성공적으로 저장되었습니다.")
    } catch {
        print(error.localizedDescription)
    }
}

// 사용 예시
let fileUrl = URL(fileURLWithPath: "path_to_your_file")
let directoryPath = "path_to_your_directory"
saveFileToDirectory(fileUrl: fileUrl, directoryPath: directoryPath)

위의 코드에서 saveFileToDirectory 함수는 주어진 파일 URL을 특정 디렉토리로 복사하는 역할을 합니다. 이 함수를 사용하여 파일을 특정 디렉토리에 저장할 수 있습니다.

참고 문헌: