๊ธ€ ์ž‘์„ฑ์ž: ๊ฐœ๋ฐœํ•˜๋Š” ํ›ˆ์ด

์•Œ๋ฉด ์“ธ๋ชจ์žˆ๋Š” ์Šค์œ„ํ”„ํŠธ ํŒ: FileManager

FileManager

FileManager๋Š” Foundation์—์„œ ์ œ๊ณตํ•˜๋Š” ํŒŒ์ผ์ž…์ถœ๋ ฅ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. ํŒŒ์ผ ์ฝ๊ธฐ, ์“ฐ๊ธฐ, ๋ณต์‚ฌ, ์‚ญ์ œ์™€ ๊ฐ™์ด ํŒŒ์ผ์ž…์ถœ๋ ฅ๊ณผ ๊ด€๋ จ๋œ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

FileManager ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ

FileManager๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋จผ์ € ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. FileManager๋Š” ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์„ ์‚ฌ์šฉํ•ด์„œ ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. FileManager์˜ default ํ”„๋กœํผํ‹ฐ์— ์ ‘๊ทผํ•˜๋ฉด ์‹ฑ๊ธ€ํ†ค ์ธ์Šคํ„ด์Šค๊ฐ€ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

let fileManager = FileManager.default

์ž‘์—…ํ•  ๊ฒฝ๋กœ ์ง€์ •

FileManager๋ฅผ ํ†ตํ•ด ์ ‘๊ทผํ•  ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

import Foundation

do {
    let desktopDirectory = FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask).first
    let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
    let libraryDirectory = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first
    let userDirectory = FileManager.default.urls(for: .userDirectory, in: .userDomainMask).first
    let downloadDirectory = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first
    let trashDirectory = FileManager.default.urls(for: .trashDirectory, in: .userDomainMask).first
} catch {
    print("Error!")
}
  • for ์— ๋“ค์–ด๊ฐ€๋Š” ๊ฐ’์€ FileManager.SearchPathDirectory ๋กœ ํŒŒ์ผ์„ ์ฐพ์„ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ง€์ •ํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์šด๋กœ๋“œ ํด๋”, ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํด๋” ๋“ฑ ๋‹ค์–‘ํ•œ ํด๋”๋ฅผ ์ง€์ •ํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • in ์— ๋“ค์–ด๊ฐ€๋Š” ๊ฐ’์€ FileManager.SearchPathDomainMask ๋กœ ํŒŒ์ผ์„ ์ฐพ์„ ์‹œ์Šคํ…œ ๋„๋ฉ”์ธ์„ ์ง€์ •ํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฒฝ๋กœ ์ถ”๊ฐ€ 

์„ค์ •๋œ ๊ธฐ๋ณธ ๊ฒฝ๋กœ์— ์ถ”๊ฐ€์ ์ธ ๊ฒฝ๋กœ๋ฅผ ์„ค์ •ํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถ”๊ฐ€ํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

let filename = "file.txt"
let filepath = documentsDirectory?.appendingPathComponent(filename)

let dirname = "dir"
let dirpath = documentsDirectory?.appendingPathComponent(dirname)
  • ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด document ๋””๋ ‰ํ† ๋ฆฌ์— ํ•˜์œ„์— ์ƒˆ๋กœ์šด ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋„๋ก ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํŒŒ์ผ ์ƒ์„ฑ

์„ค์ •ํ•œ ๊ฒฝ๋กœ์— ์ƒˆ๋กœ์šด ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด write ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํŒŒ์ผ์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

let data = "Hello World!"
try data.write(to: filepath, atomically: false, encoding: .utf8)
  • to ์—๋Š” ์ƒ์„ฑํ•œ ๊ฒฝ๋กœ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
  • atomically๋ฅผ true๋กœ ํ•˜๋ฉด ํŒŒ์ผ์„ ๋ฐฑ์—…ํŒŒ์ผ์— ๋จผ์ € ์ž‘์„ฑํ•œ ๋’ค์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์•˜์„ ๋•Œ๋งŒ ํ•ด๋‹น ํŒŒ์ผ์„ ํŒŒ์ผ๊ฒฝ๋กœ์— ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. false๋กœ ์„ค์ •ํ•˜๋ฉด ํŒŒ์ผ์„ ๊ณง๋ฐ”๋กœ ์ง€์ •ํ•œ ๊ฒฝ๋กœ์— ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ

createDirectory ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํŒŒ์ผ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋””๋ ‰ํ† ๋ฆฌ๋„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

try FileManager.default.createDirectory(at: dirpath, withIntermediateDirectories: false, attributes: nil)
  • withIntermediateDirectories๋ฅผ true๋กœ ํ•˜๋ฉด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ค‘์ฒฉ๋œ ๋””๋ ‰ํ† ๋ฆฌ ์—ญ์‹œ ๋ชจ๋‘ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. false๋กœ ์„ค์ •ํ•˜๋ฉด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ค‘์ฒฉ๋œ ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์„ค์ •๋˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

ํŒŒ์ผ ๋ณต์‚ฌ

ํŒŒ์ผ ๋ณต์‚ฌ๋Š” copyItem ๋ฉ”์„œ๋“œ๋กœ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

try FileManager.default.copyItem(atPath: srcPath, toPath: destPath)
  • atPath์— ์žˆ๋Š” ํŒŒ์ผ์„ toPath๋กœ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค.

ํŒŒ์ผ ์‚ญ์ œ

ํŒŒ์ผ ์‚ญ์ œ๋Š” removeItem ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

try FileManager.default.removeItem(at: filepath)
  • ์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด at ๊ฒฝ๋กœ์— ์žˆ๋Š” ํŒŒ์ผ์ด ์‚ญ์ œ๋ฉ๋‹ˆ๋‹ค.

ํŒŒ์ผ ์ฝ๊ธฐ

ํŒŒ์ผ์„ ์ฝ์„ ๋•Œ๋Š” FileManager๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋Š” ์•Š์ง€๋งŒ String ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ URL ํƒ€์ž…์˜ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ํ•œ๋ฒˆ์— ์ฝ์–ด์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

let data = String(contentsOf: filepath, encoding: .utf8)