簡體   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