ios - 快速复制文件

标签 ios swift restkit

我正在用这段代码复制一个文件数据库

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 Failed to copy seed database from path ...

最佳答案

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

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 的任何其他信息,我们需要有关您的代码和您尝试执行的操作的更多信息。

关于ios - 快速复制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48441271/

相关文章:

ios - 对象未保存在 Parse-Server 上

swift - UISearchResultsUpdating 不根据 searchBar 文本过滤 collectionView

Swift NSDate 扩展错误 : Mutating isn't valid on methods in classes or class-bound protocols

objective-c - 如何将 Json 字符串转换为 NSArray?

iphone - 在 Restkit 中使用 block (如 ASIHttpRequest Blocks)

RestKit 使用 sendSynchronously 方法的最佳实践

ios - 使用 NSScanner 或 NSRange 作为 url 来查找 NSSet 对象

ios - 在 iOS 8 共享扩展和主应用程序之间共享数据

iphone - 使用 00 :00:00 as time 获取今天的 NSDate

swift - 如何在警报中设置键盘类型