Swift 3.0 Class Note — 檔案系統架構

Presenter : Wei Wei

%e8%9e%a2%e5%b9%95%e5%bf%ab%e7%85%a7-2016-10-11-%e4%b8%8b%e5%8d%886-24-58

圖片來源:Wei Wei

  • 找出路徑& URL (Documents, Caches, Tmp)
  • 教兩種找出URL的方法
  • 找出路徑(ex,NSHomeDirectory() + “/Documents”) -> 產生URL
  • 或是FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0])
  • 寫入資料(String, Array, Dictionary, image)
  • 找到存檔路徑(ex., NSHomeDirectory() + “Text.txt”
    ) -> 用.write存檔
  • Array 要轉換成 NSArray 才能存檔
  • Dictionary 要轉換成 NSDictionary才能存檔
  • UIImage要先轉成Data
  • atomically: 選true的話,會先存在一個暫存檔,等確定後再存入,避免錯誤
  • 讀出資料(String, Array, Dictionary, image)
  • 找到存檔路徑 ->用 NSString(contentsOfFile) 來 load (or NSArray…etc)
  • 開新的資料夾
  • 先確定要產生的路徑 -> 用 FileManager 來產生資料夾(.createDirectory)
  • 檢視資料夾內容
  • 先確定要檢視的資料夾路徑 -> 用FileManager 找出有什麼檔案(.contentsOfDirectory)
  • 確認路徑是否為資料夾
  • 用isExist & isDirectory
  • 拷貝、移動、刪除檔案或資料夾
  • 找出原來檔案路徑
  • 給要拷貝檔案的路徑(拷貝資料夾前不加斜線!) or 要移動檔案的路徑
  • 用FileManager 來移動或拷貝檔案(.copyItem or .moveItem、removeItem)

git@github.com:dan12411/HelloFileSystem.git