iOS/코드조각

[iOS, Swift] 앱 내에 폴더 만들고 파일 저장하기(FileManager)

검은참깨두유vm 2023. 3. 4. 00:01
반응형

iOS 16.1, Xcode 14.2, Swift 5, UIKit 환경에서 진행했습니다.

 

 

폴더 생성 코드

let documentURL = FileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
let directoryURL = documentURL.appendPathComponent("folder")

do {
    try fileManager.createDirectory(atPath: directoryURL.path, withIntermediateDirectories: false, attributes: nil)
} catch let e as NSError {
    print(e.localizedDescription)
}

 

파일 삭제 코드

let fileURL = ...

if fileManager.fileExists(atPath: fileURL.path) {
    try? fileManager.removeItem(atPath: fileURL.path)
}

 

파일 복제 코드

try FileManager.default.moveItem(atPath: url.path, toPath: tempURL.path)

 

파일명 가져오는 코드

let fileName = fileURL.lastPathComponent

 

반응형