【问题标题】:Copy file with swift快速复制文件
【发布时间】:2018-07-04 14:34:50
【问题描述】:

我正在使用此代码复制文件数据库

try fileManager.copyItem(atPath: storeURL.path, toPath: storeCopyURL.path)

我可以看到创建了一个新的sqlite数据库

稍后,当我尝试使用此功能时

try! sharedInstance.managedObjectStore.addSQLitePersistentStore(atPath: storeURL.path, fromSeedDatabaseAtPath: storeCopyURL.path, withConfiguration: nil, options: nil)

我收到一个错误

E restkit.core_data:RKManagedObjectStore.m:299 复制种子失败 来自路径的数据库...

【问题讨论】:

    标签: ios swift restkit


    【解决方案1】:

    要安全地复制文件,您应该使用以下扩展名:

    extension FileManager {
    
        open func secureCopyItem(at srcURL: URL, to dstURL: URL) -> Bool {
            do {
                if FileManager.default.fileExists(atPath: dstURL.path) {
                    try FileManager.default.removeItem(at: dstURL)
                }
                try FileManager.default.copyItem(at: srcURL, to: dstURL)
            } catch (let error) {
                print("Cannot copy item at \(srcURL) to \(dstURL): \(error)")
                return false
            }
            return true
        }
    
    }
    

    对于有关 CoreData 的任何其他信息,我们需要有关您的代码以及您正在尝试做什么的更多信息。

    【讨论】:

    • @Maulikshah ,您可以将此扩展名用作 FileManager.default.secureCopyItem(at: , to: )
    • 为什么要创建FileManager 的实例方法只是为了调用静态default FileManager 的函数?我建议您删除方法调用的FileManager.default. 部分,并改为调用self 实例的方法。
    • 我收到此错误:“文档”无法删除,因为您无权访问它。”
    猜你喜欢
    • 1970-01-01
    • 2014-03-31
    • 1970-01-01
    • 2018-02-12
    • 2011-12-15
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    相关资源
    最近更新 更多