gpt4 book ai didi

objective-c - Swift:无法预加载 Coredata

转载 作者:可可西里 更新时间:2023-11-01 00:53:30 26 4
gpt4 key购买 nike

当我在“目标 - 构建阶段 - 复制捆绑资源”下包含带有 Objective-C 的 SQLite 文件时,该文件将被完全复制到目标,即设备或模拟器。在目标上,我得到了整个文件:表和内容(记录/行)。

用 Swift 做同样的事情,表格将被复制,但它们是空的:没有记录/行。 :-(

我可以做一些额外的事情吗?有什么“绝招”吗?

我如何使用 Swift 将基本记录预加载到我的核心数据???

我使用的是 Xcode v6.1.1,与 Beta 6.2 是一样的。

最佳答案

这是我的解决方案(针对 sqlite 文件)。不知道为什么在Swift里这么“奢侈”。也许有更简单的方法?感谢 pbasdf !!!

重要:在 *.sqlite 文件下,您必须将 *.sqlite-shm 和 *.sqlite-wal 添加到您的项目中这个文件会自动添加到“Target - Build Phases - Copy Bundle Resources”

这是基于“AppDelegate.swift”的标准模板

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
// Create the coordinator and store
var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)

let dataName = „MyData.sqlite" // must be replaced by the name of your file!
let modelName = „MyData“ // must be replaced by the name of your model!

let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent(dataName)

let fileManager = NSFileManager.defaultManager()
let bundle = NSBundle.mainBundle()
let fromURL = bundle.URLForResource(modelName, withExtension: "sqlite")

// check sqlite-file: must it be installed?
if !fileManager.fileExistsAtPath(url.path!) {
self.copySqlliteFiles(modelName, databaseName: dataName)
}

var error: NSError? = nil
var failureReason = "There was an error creating or loading the application's saved data."

if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {

...

   return coordinator
}()

// MARK: - copy sqllite-files (c) by Ray Wenderlich & Team in „“Core Data by Tutorials“

// check sqlite-files: they must be installed...
func copySqlliteFiles(modelName: String, databaseName: String)
{
let bundle = NSBundle.mainBundle()

let baseDatabaseURL = bundle.URLForResource(modelName, withExtension: "sqlite")
let documentsURL = self.applicationDocumentsDirectory
let storeURL = documentsURL.URLByAppendingPathComponent(databaseName)
NSFileManager.defaultManager().copyItemAtURL(baseDatabaseURL!, toURL: storeURL,error: nil)

let baseSHMURL = bundle.URLForResource(modelName,withExtension: "sqlite-shm")
let shmURL = self.applicationDocumentsDirectory.URLByAppendingPathComponent(databaseName + "-shm")
NSFileManager.defaultManager().copyItemAtURL(baseSHMURL!, toURL: shmURL, error: nil)

let walURL = self.applicationDocumentsDirectory.URLByAppendingPathComponent(databaseName + "-wal")
let baseWALURL = bundle.URLForResource(modelName, withExtension: "sqlite-wal")
NSFileManager.defaultManager().copyItemAtURL(baseWALURL!, toURL: walURL, error: nil)
}

关于objective-c - Swift:无法预加载 Coredata,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27841095/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com