如何使用swift 3 xcode 8预加载核心数据库

我有一个数据库与数据,我想预先在应用程序中。 在swift 3之前,它的工作原理和我遵循本教程: http : //www.appcoda.com/core-data-preload-sqlite-database/但如何加载相同的数据库为SWIFT 3? 在介绍NSPersistentContainer ,如何加载我项目中的.sqlite文件?

实际上,创build数据库的默认path在swift中更改为3。 所以现在的代码看起来像这样:

 func preloadDBData() { let sqlitePath = Bundle.main.path(forResource: "MyDB", ofType: "sqlite") let sqlitePath_shm = Bundle.main.path(forResource: "MyDB", ofType: "sqlite-shm") let sqlitePath_wal = Bundle.main.path(forResource: "MyDB", ofType: "sqlite-wal") let URL1 = URL(fileURLWithPath: sqlitePath!) let URL2 = URL(fileURLWithPath: sqlitePath_shm!) let URL3 = URL(fileURLWithPath: sqlitePath_wal!) let URL4 = URL(fileURLWithPath: NSPersistentContainer.defaultDirectoryURL().relativePath + "/MyDB.sqlite") let URL5 = URL(fileURLWithPath: NSPersistentContainer.defaultDirectoryURL().relativePath + "/MyDB.sqlite-shm") let URL6 = URL(fileURLWithPath: NSPersistentContainer.defaultDirectoryURL().relativePath + "/MyDB.sqlite-wal") if !FileManager.default.fileExists(atPath: NSPersistentContainer.defaultDirectoryURL().relativePath + "/MyDB.sqlite") { // Copy 3 files do { try FileManager.default.copyItem(at: URL1, to: URL4) try FileManager.default.copyItem(at: URL2, to: URL5) try FileManager.default.copyItem(at: URL3, to: URL6) print("=======================") print("FILES COPIED") print("=======================") } catch { print("=======================") print("ERROR IN COPY OPERATION") print("=======================") } } else { print("=======================") print("FILES EXIST") print("=======================") } } 

现在你可以从AppDelegate的didFinishLaunchWithOptions方法中调用这个方法,这将预加载我们在应用程序中放入的数据库。