使用iOS应用程序传送核心数据
我正在开发一个字典应用程序,里面有大约10000个单词。我使用核心数据存储所有数据,我使用的语言是swift 我所要做的就是将这些数据与应用程序一起发送,这样当应用程序从应用程序商店下载时,它就包含了所有数据 我已经对此进行了搜索,发现可以通过将SQLite文件包含到项目中来实现。但我真的不知道怎么做,也不知道El Capitan的模拟器目录在哪里 我只是iOS的初学者,所以请有人用非常简单的步骤向我解释一下使用iOS应用程序传送核心数据,ios,iphone,core-data,swift2,osx-elcapitan,Ios,Iphone,Core Data,Swift2,Osx Elcapitan,我正在开发一个字典应用程序,里面有大约10000个单词。我使用核心数据存储所有数据,我使用的语言是swift 我所要做的就是将这些数据与应用程序一起发送,这样当应用程序从应用程序商店下载时,它就包含了所有数据 我已经对此进行了搜索,发现可以通过将SQLite文件包含到项目中来实现。但我真的不知道怎么做,也不知道El Capitan的模拟器目录在哪里 我只是iOS的初学者,所以请有人用非常简单的步骤向我解释一下 将sqlite文件添加到项目中 你会打电话的 [[NSFileManager def
[[NSFileManager defaultManager] copyItemAtURL:preloadURL toURL:storeURL error:&error]
[_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:nil
error:&error]
其中,preload URL
将是应用程序包中文件的URL。storeURL
是核心数据的sqlite文件路径(通常在应用程序目录中)
因此,如果该文件不存在,则该文件将从bundle复制到app目录,否则将失败
[[NSFileManager defaultManager] copyItemAtURL:preloadURL toURL:storeURL error:&error]
[_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:nil
error:&error]
其中,preload URL
将是应用程序包中文件的URL。storeURL
是核心数据的sqlite文件路径(通常在应用程序目录中)
因此,如果文件不存在,将从bundle复制到app目录,否则将失败。这可能是一个很长的答案。最好将其作为教程进行解释。下面的链接将告诉您该怎么做 我发现这篇文章/教程的开头和结尾对您正在尝试做的事情很有用 我最近将数据预加载到一个应用程序中进行测试,使用一个单独的Xcode模拟器创建所需的3个SQLite文件。这篇文章将向您展示如何找到它们,如何将它们“捆绑”到您想要“预加载”的Xcode项目中,并告诉您要在“AppDelegate.swift”文件中添加的代码以及位置 有三件事可以帮助你在完成项目时避免问题
lazy var managedObjectModel: NSManagedObjectModel = {
// The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
let modelURL = NSBundle.mainBundle().URLForResource("EoMQ", withExtension: "momd")!
return NSManagedObjectModel(contentsOfURL: modelURL)!
}()
// ---------------------------------------------------
// Create the coordinator and store
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("Q100cdCoreData.sqlite") // originally = "SingleViewCoreData.sqlite" - then changed to new name... from imported sqlite files
// ---------------------------------------------------
// ** Load the Already made DB from Simulator **
if !NSFileManager.defaultManager().fileExistsAtPath(url.path!) {
let sourceSqliteURLs = [NSBundle.mainBundle().URLForResource("Q100cdCoreData", withExtension: "sqlite")!, NSBundle.mainBundle().URLForResource("Q100cdCoreData", withExtension: "sqlite-wal")!, NSBundle.mainBundle().URLForResource("Q100cdCoreData", withExtension: "sqlite-shm")!]
let destSqliteURLs = [self.applicationDocumentsDirectory.URLByAppendingPathComponent("Q100cdCoreData.sqlite"), self.applicationDocumentsDirectory.URLByAppendingPathComponent("Q100cdCoreData.sqlite-wal"), self.applicationDocumentsDirectory.URLByAppendingPathComponent("Q100cdCoreData.sqlite-shm")]
for index in 0 ..< sourceSqliteURLs.count {
do {
try NSFileManager.defaultManager().copyItemAtURL(sourceSqliteURLs[index], toURL: destSqliteURLs[index])
} catch {
print(error)
}
}
}
别管它,但直接在它下面你需要有以下代码…
lazy var managedObjectModel: NSManagedObjectModel = {
// The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
let modelURL = NSBundle.mainBundle().URLForResource("EoMQ", withExtension: "momd")!
return NSManagedObjectModel(contentsOfURL: modelURL)!
}()
// ---------------------------------------------------
// Create the coordinator and store
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("Q100cdCoreData.sqlite") // originally = "SingleViewCoreData.sqlite" - then changed to new name... from imported sqlite files
// ---------------------------------------------------
// ** Load the Already made DB from Simulator **
if !NSFileManager.defaultManager().fileExistsAtPath(url.path!) {
let sourceSqliteURLs = [NSBundle.mainBundle().URLForResource("Q100cdCoreData", withExtension: "sqlite")!, NSBundle.mainBundle().URLForResource("Q100cdCoreData", withExtension: "sqlite-wal")!, NSBundle.mainBundle().URLForResource("Q100cdCoreData", withExtension: "sqlite-shm")!]
let destSqliteURLs = [self.applicationDocumentsDirectory.URLByAppendingPathComponent("Q100cdCoreData.sqlite"), self.applicationDocumentsDirectory.URLByAppendingPathComponent("Q100cdCoreData.sqlite-wal"), self.applicationDocumentsDirectory.URLByAppendingPathComponent("Q100cdCoreData.sqlite-shm")]
for index in 0 ..< sourceSqliteURLs.count {
do {
try NSFileManager.defaultManager().copyItemAtURL(sourceSqliteURLs[index], toURL: destSqliteURLs[index])
} catch {
print(error)
}
}
}
//---------------------------------------------------
//创建协调器和存储
let coordinator=NSPersistentStoreCoordinator(managedObjectModel:self.managedObjectModel)
让url=self.applicationDocumentsDirectory.URLByAppendingPathComponent(“Q100cdCoreData.sqlite”)//最初为=“SingleViewCoreData.sqlite”-然后更改为新名称。。。从导入的sqlite文件
// ---------------------------------------------------
//**从模拟器加载已生成的数据库**
如果!NSFileManager.defaultManager().fileExistsAtPath(url.path!){
让sourceSqliteURLs=[NSBundle.mainBundle().URLForResource(“Q100cdCoreData”,带扩展名:“sqlite”)!,NSBundle.mainBundle().URLForResource(“Q100cdCoreData”,带扩展名:“sqlite shm”)!,NSBundle.mainBundle().URLForResource(“Q100cdCoreData”,带扩展名:“sqlite shm”)!]
设destSqliteURLs=[self.applicationDocumentsDirectory.URLByAppendingPathComponent(“Q100Ccoredata.sqlite”)、self.applicationDocumentsDirectory.URLByAppendingPathComponent(“Q100Ccoredata.sqlite”)、self.applicationDocumentsDirectory.URLByAppendingPathComponent(“Q100Ccoredata.sqlite shm”)]
对于0中的索引。
我在模拟器中创建的3个文件名为“Q100cdCoreData”,但有三个不同的扩展名。。。(a) .sqlite(b)、wal(c)、shm
但是您需要阅读本教程并理解过程。这可能是一个相当长的答案。最好将其作为教程进行解释。下面的链接将告诉您该怎么做 我发现这篇文章/教程的开头和结尾对您正在尝试做的事情很有用 我最近将数据预加载到一个应用程序中进行测试,使用一个单独的Xcode模拟器创建所需的3个SQLite文件。这篇文章将向您展示如何找到它们,如何将它们“捆绑”到您想要“预加载”的Xcode项目中,并告诉您要在“AppDelegate.swift”文件中添加的代码以及位置 有三件事可以帮助你在完成项目时避免问题