ios – 无法在路径上打开领域

ios – 无法在路径上打开领域,第1张

概述我试图使用捆绑的域文件,但没有成功.我知道我的领域文件已成功复制到我的应用程序目录但我无法读取它. fatal error: ‘try!’ expression unexpectedly raised an error: “Unable to open a realm at path ‘/Users/…/Library/Developer/CoreSimulator/Devices/…/data/ 我试图使用捆绑的域文件,但没有成功.我知道我的领域文件已成功复制到我的应用程序目录但我无法读取它.

Fatal error: ‘try!’ Expression unexpectedly raised an error: “Unable
to open a realm at path
‘/Users/…/library/Developer/CoreSimulator/Devices/…/data/Containers/Data/Application/…/documents/default-v1.realm’.
Please use a path where your app has read-write permissions.”

func fullPathTofileInAppdocumentsDir(filename: String) -> String {        let paths = NSSearchPathForDirectorIEsInDomains(.documentDirectory,NSSearchPathDomainMask.UserDomainMask,true)        let documentsDirectory = paths[0] as Nsstring        let fullPathToThefile = documentsDirectory.stringByAppendingPathComponent(filename)        return fullPathToThefile    }

在dIDFinishLaunchingWithOptions中:

let fileIndocuments = fullPathTofileInAppdocumentsDir("default-v1.realm")        if !NSfileManager.defaultManager().fileExistsAtPath(fileIndocuments) {            let bundle = NSBundle.mainBundle()            let fileInBundle = bundle.pathForResource("default-v1",ofType: "realm")            let fileManager = NSfileManager.defaultManager()            do {                try fileManager.copyItemAtPath(fileInBundle!,topath: fileIndocuments)            } catch {  print(error)  }        }

并设置用于默认Realm的配置:

var config = Realm.Configuration()config.path = fileIndocumentsRealm.Configuration.defaultConfiguration = configlet realm = try! Realm(configuration: config)  // It fails here!!! :-)

正如文档所示,我也试过通过在Realm.Configuration对象上将Readonly设置为true来直接从bundle路径打开它.我不确定这是否与Realm有关,或者我是否忽略了文件系统的某些内容….我还尝试将文件存储在library文件夹中.

Realm 0.97.0Xcode Version 7.1.1
解决方法 我尝试使用Realm的浏览器应用程序打开领域文件,该文件不再打开.它现在有新的权限:只写(DropBox).因此,我决定使用文件管理器的 setAttributes方法将文件权限更改为读/写.我是这样做的:

// rw rw r : Attention for octal-literal in Swift "0o". let permission = NSNumber(short: 0o664)  do {    try fileManager.setAttributes([NSfilePosixPermissions:permission],ofItemAtPath: fileIndocuments)  } catch { print(error) }

现在可以在此路径中打开域文件.

总结

以上是内存溢出为你收集整理的ios – 无法在路径上打开领域全部内容,希望文章能够帮你解决ios – 无法在路径上打开领域所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1048393.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存