![](/img/trans.png)
[英]Copy Folder (w/contents) from bundle to Documents directory - iOS
[英]Swift 3 - Copy Folder w/ contents from Main Bundle to Documents Directory
我的主要包中包含文件夾,我想在首次啟動應用程序時將它們復制/剪切到文檔目錄,以便從那里訪問它們。 我見過一些例子,但是他們都在Obj-C中,我正在使用Swift 3.我怎么能這樣做?
我設法使用2個功能:
func copyFolders() {
let filemgr = FileManager.default
filemgr.delegate = self
let dirPaths = filemgr.urls(for: .documentDirectory, in: .userDomainMask)
let docsURL = dirPaths[0]
let folderPath = Bundle.main.resourceURL!.appendingPathComponent("Test").path
let docsFolder = docsURL.appendingPathComponent("Test").path
copyFiles(pathFromBundle: folderPath, pathDestDocs: docsFolder)
}
func copyFiles(pathFromBundle : String, pathDestDocs: String) {
let fileManagerIs = FileManager.default
fileManagerIs.delegate = self
do {
let filelist = try fileManagerIs.contentsOfDirectory(atPath: pathFromBundle)
try? fileManagerIs.copyItem(atPath: pathFromBundle, toPath: pathDestDocs)
for filename in filelist {
try? fileManagerIs.copyItem(atPath: "\(pathFromBundle)/\(filename)", toPath: "\(pathDestDocs)/\(filename)")
}
} catch {
print("\nError\n")
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.