【发布时间】:2017-04-21 14:39:13
【问题描述】:
我想复制预加载的领域文件,所以我这样做了:
将我的文件复制到名为 default.realm 的项目导航器。然后检查fileExists::
let bundlePath = Bundle.main.path(forResource: "default", ofType: "realm")
let destPath = Realm.Configuration.defaultConfiguration.fileURL?.path
let fileManager = FileManager.default
if fileManager.fileExists(atPath: destPath!) {
//File exist, do nothing
print("File exist")
} else {
do {
//Copy file from bundle to Realm default path
try fileManager.copyItem(atPath: bundlePath!, toPath: destPath!)
print("Copied")
} catch {
print("\n",error)
}
}
而且它总是返回 true,所以我不能复制我的领域文件。
【问题讨论】:
-
您有代码
let realm = try! Realm()作为视图控制器中的实例变量吗?视图控制器在didFinishLaunchingWithOptions之前从 Storyboard 实例化。请确认并将其修改为lazy var realm = try! Realm(),然后从模拟器中删除该应用程序,然后重试。 -
@kishikawakatsumi 非常感谢。所有问题都在
let!