首先,我对 Realm
生态系统还很陌生。
我已经在 AppDelegate
的 applicationDidFinishLaunching
方法中试过了,
print("Realm Path : \(realm.configuration.fileURL?.absoluteURL)")
但它给了我错误,因为我向 realm
对象添加了一些新属性,这与旧模式产生了冲突,甚至在打印之前就使应用程序崩溃了。
如果您对崩溃日志感兴趣。但这与主要问题无关,对吧?
fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=10 "Migration is required due to the following errors: - Property 'id' has been added to latest object model." UserInfo={NSLocalizedDescription=Migration is required due to the following errors: - Property 'id' has been added to latest object model., Error Code=10}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-703.0.18.8/src/swift/stdlib/public/core/ErrorType.swift, line 54
我还有一个疑问。我已经添加了这个, Realm 必须在初始化旧对象之前 deleteAll
对吗?但它只是因上面给出的日志而崩溃。
import Cocoa
import RealmSwift
import Realm
//MARK: Global Variables
public let realm = try! Realm()
func applicationDidFinishLaunching(aNotification: NSNotification) {
realm.deleteAll()
print("Realm Path : \(realm.configuration.fileURL?.absoluteURL)")
}
所以现在我想删除 realm
文件,但我不知道在哪里可以找到它。我在 Mac 中使用扩展名 .realm
进行了搜索,但找不到预期的文件。我有 Realm 浏览器
应用程序,我以为它会将我重定向到文件位置,但没有,它只是打开了 Documents
文件夹,没有 realm 文件.
你会怎么做?提前谢谢。
最佳答案
当您创建新文件时, Realm
会发生变化。
只需将以下行添加到您的 ViewDidLoad
方法中:
print(Realm.Configuration.defaultConfiguration.fileURL!)
此行会将位置打印到 XCode 控制台。复制文件路径,转到 Finder
-> Go -> Go go Folder... -> 粘贴路径并点击 Go。
关于swift - 如何找到 Mac App 的 Realm 文件位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38279615/