我想在应用程序在设备上的第一顿午餐时显示持久性数据,以便用户可以看到它,并且我将领域用作我的数据库。
我在模拟器上用数据填充文件后被欺骗,现在需要将数据文件与应用程序合并或捆绑在一起。现在,我将default.realm文件复制到dcouments中,然后从AppDelegate中的stackoverflow复制了一些相关代码以将其捆绑在一起,但是失败了!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
openRealm() //2
print(Realm.Configuration.defaultConfiguration.fileURL) //3
return true
}
//1
func openRealm() {
let bundlePath = Bundle.main.path(forResource: "default", ofType: "realm")!
let defaultPath = Realm.Configuration.defaultConfiguration.fileURL!.path
let fileManager = FileManager.default
// Only need to copy the prepopulated `.realm` file if it doesn't exist yet
if !fileManager.fileExists(atPath: defaultPath){
print("use pre-populated database")
do {
try fileManager.copyItem(atPath: bundlePath, toPath: defaultPath)
print("Copied")
} catch {
print(error)
}
}
} //f
当我构建应用程序时,总会崩溃,并且Xcode告诉我值是零
线程1:致命错误:展开一个可选值时意外地找到nil
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句