我试图使用捆绑的领域文件但没有成功。我知道我的领域文件已成功复制到我的应用程序目录,但我无法读取它。
致命错误:“尝试!” 表达式意外引发错误:“无法在路径 '/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: config) // It fails here!!! :-)
Run Code Online (Sandbox Code Playgroud)
正如文档所建议的那样,我也尝试通过在对象上设置readOnly为直接从包路径打开它。我不确定这是否与 Realm 相关,或者我是否忽略了文件系统的某些内容...... 我还尝试将文件存储在 Library 文件夹中。trueRealm.Configuration
Realm 0.97.0
Xcode Version 7.1.1
Run Code Online (Sandbox Code Playgroud)
我尝试使用 Realm 的浏览器应用程序打开领域文件,但该文件不再打开。它现在拥有新的权限:Write only (Dropbox). 因此,我决定将文件权限更改回read/write使用文件管理器\xe2\x80\x99s setAttributes方法。我是这样做的:
// rw rw r : Attention for octal-literal in Swift "0o".\n let permission = NSNumber(short: 0o664)\n\n do {\n try fileManager.setAttributes([NSFilePosixPermissions:permission], ofItemAtPath: fileInDocuments)\n\n } catch { print(error) }\nRun Code Online (Sandbox Code Playgroud)\n\n现在可以在此路径中打开领域文件。
\n| 归档时间: |
|
| 查看次数: |
9264 次 |
| 最近记录: |