【问题标题】:How can i copy Folder from Project to Document Directory ios swift我如何将文件夹从项目复制到文档目录 ios swift
【发布时间】:2018-11-30 10:10:43
【问题描述】:

这是我的文件夹结构

我想将此 Stickers 文件夹复制到 文档目录,为此我使用了以下代码

    let fileManager = FileManager.default

    let documentsUrl = fileManager.urls(for: .documentDirectory,
                                        in: .userDomainMask)

    guard documentsUrl.count != 0 else {
        return // Could not find documents URL
    }

    let finalDatabaseURL = documentsUrl.first!.appendingPathComponent("Stickers")

    if !( (try? finalDatabaseURL.checkResourceIsReachable()) ?? false) {
        print("DB does not exist in documents folder")

        let documentsURL = Bundle.main.resourceURL?.appendingPathComponent("Stickers")

        do {
            try fileManager.copyItem(atPath: (documentsURL?.path)!, toPath: finalDatabaseURL.path)
        } catch let error as NSError {
            print("Couldn't copy file to final location! Error:\(error.description)")
        }

    } else {
        print("Database file found at path: \(finalDatabaseURL.path)")
    }

但它不会工作。我收到一个错误

注意:如果无法复制文件夹,那么我想将 Assets.xcassets 复制到 Document Directory。我怎样才能实现它?

【问题讨论】:

    标签: ios mobile directory swift4 document-directory


    【解决方案1】:

    请在下面的代码.. 我在两个函数中更新了您的代码,以将所有文件从文件夹复制到文档目录。

    希望它会起作用。

    func copyFolders() {
        let fileManager = FileManager.default
    
        let documentsUrl = fileManager.urls(for: .documentDirectory,
                                            in: .userDomainMask)
    
        guard documentsUrl.count != 0 else {
            return // Could not find documents URL
        }
    
        let finalDatabaseURL = documentsUrl.first!.appendingPathComponent("Stickers")
    
        if !( (try? finalDatabaseURL.checkResourceIsReachable()) ?? false) {
            print("DB does not exist in documents folder")
    
            let documentsURL = Bundle.main.resourceURL?.appendingPathComponent("Stickers")
    
            do {
                if !FileManager.default.fileExists(atPath:(finalDatabaseURL?.path)!)
                {
                    try FileManager.default.createDirectory(atPath: (finalDatabaseURL.path), withIntermediateDirectories: false, attributes: nil)
                }
                copyFiles(pathFromBundle: (documentsURL?.path)!, pathDestDocs: finalDatabaseURL.path)
            } catch let error as NSError {
                print("Couldn't copy file to final location! Error:\(error.description)")
            }
    
        } else {
            print("Database file found at path: \(finalDatabaseURL.path)")
        }
    
    }
    
    func copyFiles(pathFromBundle : String, pathDestDocs: String) {
        let fileManagerIs = FileManager.default
        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")
        }
    }
    

    【讨论】:

    • 嘿,我的文件夹已复制到 Document 文件夹中,但我还想要我文件夹中的数据,请您帮忙
    • 此代码在文档目录中创建文件夹,但它没有在此文件夹中复制我的贴纸
    • 你应该从这个文件夹中选择一个文件。不要复制文件夹,并且在目录端必须给出您复制的文件名。
    • 我更新了一个代码...检查并使用它。只需调用 ---> copyFolders()
    • 抱歉,我在 -->if !FileManager.default.fileExists(atPath:(finalDatabaseURL?.path)!) 中检查了捆绑路径。我更新了它......现在试试。希望它会工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    • 2013-01-06
    • 2013-07-23
    • 2016-06-23
    相关资源
    最近更新 更多