繁体   English   中英

将所有捆绑软件资源复制到Documents目录内的单独文件夹中

[英]Copy all the bundle resources to a separate folder inside the Documents directory

复制包中包含的所有文件 (而不是[NSBundle mainBundle] )并将它们放置到Documents目录中新创建的目录中的正确方法是什么?

您需要一个一个地复制项目,这非常简单。

let bundle: Bundle = ... // Whatever bundle you want to copy from
    guard let resourceURL = bundle.resourceURL else { return }
let fileManager = FileManager.default
do {
    let documentsDirectory = try fileManager.url(for: .documentDirectory,
                                                 in: .userDomainMask,
                                                 appropriateFor: nil,
                                                 create: false)
    let destination = documentsDirectory.appendingPathComponent("BundleResourcesCopy", isDirectory: true)

    var isDirectory: ObjCBool = false
    if fileManager.fileExists(atPath: destination.path, isDirectory: &isDirectory) {
        assert(isDirectory.boolValue)
    } else {
        try fileManager.createDirectory(at: destination, withIntermediateDirectories: false)
    }

    let resources = try fileManager.contentsOfDirectory(at: resourceURL, includingPropertiesForKeys: nil)
    for resource in resources {
        print("Copy \(resource) to \(destination.appendingPathComponent(resource.lastPathComponent))")
        try fileManager.copyItem(at: resource,
                                 to: destination.appendingPathComponent(resource.lastPathComponent))
    }
} catch {
    print(error)
}

根据捆绑软件的大小,这可能需要一些时间才能执行,因此您可能需要在后台线程上执行此操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM