iOS 開發 Swift 透過FileManager取得App中的文件路徑

【 Swift 】

在iOS的開發中有時候需要對本地文件(iOS內部)操作,這時候可以透過 FileManager取得我們想要的資料。FileManager不僅可以讓我們做到查詢資料,還可以對這些資料文件做刪除修改。

A file manager object lets you examine the contents of the file system and make changes to it. The FileManager class provides convenient access to a shared file manager object that is suitable for most types of file-related manipulations. A file manager object is typically your primary mode of interaction with the file system. You use it to locate, create, copy, and move files and directories. You also use it to get information about a file or directory or change some of its attributes.

技術相關 :FileManager iOS Swift

透過以下方式可以取得FileManager搜尋程式中的檔案


let manager = FileManager.default

let urlForDocument = manager.urls(for: .documentDirectory, in:.userDomainMask)

let finalurl = urlForDocument.first! as URL

let contentsOfPath = try? manager.contentsOfDirectory(atPath: finalurl.path)

print("contentsOfPath: \(contentsOfPath)")

此段程式搜尋.documentDirectory,也可以搜尋程式下的.libraryDirectory