swift - 如何找到 Mac App 的 Realm 文件位置

标签 swift macos cocoa realm

首先,我对 Realm 生态系统还很陌生。

我已经在 AppDelegateapplicationDidFinishLaunching 方法中试过了,

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/

相关文章:

php - brew install php56-mongo 命令不工作

objective-c - CGEvent NSKeyDown 仅在应用程序位于最前面时才起作用?

swift - 快速设置我的条形图的水平滚动

ios - Swift - webView 顶部的 iAd 横幅

swift - 快速测试 ping 和服务器可用性

xcode - 我可以在 macOS 10.12 上上传 Xcode 版本吗

swift - 如何获取更改UISlider的值

macos - 如何在mac上安装/启动neo4j?

objective-c - STAssertEqualStrings 中的转义序列

cocoa - 是否有一种更节省内存的方法来搜索核心数据数据库?