小编Fab*_*bio的帖子

无法在路径上打开领域

我试图使用捆绑的领域文件但没有成功。我知道我的领域文件已成功复制到我的应用程序目录,但我无法读取它。

致命错误:“尝试!” 表达式意外引发错误:“无法在路径 '/Users/.../Library/Developer/CoreSimulator/Devices/.../data/Containers/Data/Application/.../Documents/default-v1.realm' 处打开领域。请使用您的应用具有读写权限的路径。”

 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
    }
Run Code Online (Sandbox Code Playgroud)

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)  }
        }
Run Code Online (Sandbox Code Playgroud)

并设置用于默认领域的配置:

var config = Realm.Configuration()
config.path = fileInDocuments

Realm.Configuration.defaultConfiguration = config

let realm = try! Realm(configuration: …
Run Code Online (Sandbox Code Playgroud)

realm ios swift

5
推荐指数
1
解决办法
9264
查看次数

标签 统计

ios ×1

realm ×1

swift ×1