gpt4 book ai didi

ios - Swift 3 - 将包含内容的文件夹从主包复制到文档目录

转载 作者:技术小花猫 更新时间:2023-10-29 11:18:29 24 4
gpt4 key购买 nike

我的主包中有包含文件的文件夹,我想在应用程序首次启动时将它们复制/剪切到文档目录,以便从那里访问它们。我看过示例,但它们都在 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 - Swift 3 - 将包含内容的文件夹从主包复制到文档目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44434428/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com