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 – 无法在路径上打开领域所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)