我想複製預裝的境界文件,所以我做的:境界FILEEXISTS始終是真實的
複製我的文件,以項目導航命名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()'作爲視圖控制器中的一個實例變量?視圖控制器在Storyboard BEFORE'didFinishLaunchingWithOptions'中實例化。請確保並修改它們到'lazy var realm = try! Realm()',然後從模擬器中刪除應用程序,然後重新嘗試。 –
@kishikawakatsumi非常感謝你。所有問題都在'let'! – aaisataev