ios – Swift 3 – 将文件夹从主包复制到文档目录

前端之家收集整理的这篇文章主要介绍了ios – Swift 3 – 将文件夹从主包复制到文档目录前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的主要包中包含文件夹,我想在首次启动应用程序时将它们复制/剪切到文档目录,以便从那里访问它们.我见过一些例子,但他们都在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")
    }
}

猜你在找的iOS相关文章