1

从 Swift 1 迁移到 Swift 2 以及迁移 Realm 时的另一个问题......在旧代码中,我有这行代码就像一个魅力:

ApplicationController.A._initRealm = Realm.init(path: NSBundle.mainBundle().pathForResource("initRealm",ofType:"realm")!, readOnly: true, encryptionKey: nil, error: &error)

在迁移过程之后,我将这行代码替换为:

do
{
    ApplicationController.A._initRealm = try Realm.init(path: NSBundle.mainBundle().pathForResource("initRealm", ofType: "realm")!)
}
catch
{
    print(error)
}

但这不再起作用。我看到以下错误消息:

Error Domain=io.realm Code=1 "open() failed: Operation not permitted" UserInfo={Error Code=1, NSLocalizedDescription=open() failed: Operation not permitted}

我不明白为什么这不起作用。我很确定该文件是在捆绑包中找到的,所以这似乎是一个安全问题?

4

1 回答 1

2

如果 Realm 位于您的应用程序包中,您需要以只读方式打开它,因为应用程序包中的文件不可写。您在旧代码片段中通过 执行此操作readOnly: true,但您没有在新代码片段中执行等效操作。您可以在 Realm 文档的Other Realms 部分的示例代码中看到这一点。

于 2016-01-13T00:04:19.630 回答