Swift 3-将包含内容的文件夹从主捆绑包复制到文档目录 [英] Swift 3 - Copy Folder w/ contents from Main Bundle to Documents Directory
本文介绍了Swift 3-将包含内容的文件夹从主捆绑包复制到文档目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的主捆绑包中有一些文件夹,其中有文件,我想在应用程序首次启动时将其复制/剪切到文档目录"中,以便从那里访问它们.我已经看过示例,但是它们全都在Obj-C中,并且我正在使用Swift3.如何做到这一点?
I have folders with files inside them in my main bundle, and I want to copy/cut them to the Documents Directory at first launch of the application to access them from there. I've seen examples but they're all in Obj-C and I'm using Swift 3. How can I do this?
推荐答案
我设法使用2个函数来做到这一点:
I managed to do it using 2 functions:
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")
}
}
这篇关于Swift 3-将包含内容的文件夹从主捆绑包复制到文档目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文