sho*_*hoe 4 realm ios realm-mobile-platform
我正在像这样初始化我的领域实例:
private static let sUserRealm:Realm = try! Realm(configuration:Realm.Configuration(
fileURL: Bundle.main.url(forResource: "user" ,withExtension: "realm"),
inMemoryIdentifier: nil,
syncConfiguration: nil,
encryptionKey: nil,
readOnly: false,
schemaVersion: 0,
migrationBlock: sMigrationBlock,
deleteRealmIfMigrationNeeded: true,
objectTypes: nil))
Run Code Online (Sandbox Code Playgroud)
但是,我收到此错误:
fatal error: A Realm Configuration must specify a path or an in-memory
identifier.: file /Users/realm/workspace/Package iOS Swift/tightdb_objc/RealmSwift/RealmConfiguration.swift, line 201
Run Code Online (Sandbox Code Playgroud)
所有领域在关于创建多个领域的 swift 文档中都有这个例子,即使逐字复制也会引发相同的错误。如何创建和访问领域文件?
Bundle.url(forResource:withExtension:)返回给定包中现有文件的路径。您链接到的 Realm 文档部分使用了在应用程序包中包含 Realm 文件的示例。
因为它看起来像你试图创建一个新的特定路径领域的文件,你应该计算这条道路,并将其设置为fileURL上Realm.Configuration。您通常不想为此使用BundleAPI,因为您的应用程序包中的文件不可写。相反,您应该构建一个相对于应用程序文档或缓存目录的路径:
let documentDirectory = FileManager.default.url(for: .documentDirectory, in: .userDomainMask,
appropriateFor: nil, create: false)
let url = documentDirectory.appendingPathComponent("my-new-realm.realm")
Run Code Online (Sandbox Code Playgroud)
斯威夫特 5:
只需自定义您的领域文件的名称
struct Kingdom {
static let persist = Kingdom()
let realm: Realm = { () -> Realm in
var config = Realm.Configuration()
config.fileURL = config.fileURL!.deletingLastPathComponent().appendingPathComponent("deng.realm")
let land = try! Realm(configuration: config)
return land
}()
}
Run Code Online (Sandbox Code Playgroud)
自定义您的领域文件的名称和路径
struct Kingdom {
static let persist = Kingdom()
let realm: Realm = { () -> Realm in
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
let docURL = URL(string: documentsDirectory)!
let dataPath = docURL.appendingPathComponent("Dng")
if FileManager.default.fileExists(atPath: dataPath.absoluteString) == false{
do {
try FileManager.default.createDirectory(atPath: dataPath.absoluteString, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error.localizedDescription)
}
}
do {
var config = Realm.Configuration()
config.fileURL = dataPath.appendingPathComponent("deng.realm")
let realmReal = try Realm(configuration: config)
return realmReal
} catch {
print(error.localizedDescription)
let realmForSyntax = try! Realm()
return realmForSyntax
}
}()
}
Run Code Online (Sandbox Code Playgroud)
使用
Bundle.main.url(forResource: "user" ,withExtension: "realm")!但该值可能是nil因为您的文件不存在
代替
Bundle.main.url(forResource: "user" ,withExtension: "realm")
小心并设置
编辑
要使用预定义数据库,您可以使用Bundle.
但如果您想为您的应用程序使用新数据库,请使用其他路径。不是捆绑。
https://realm.io/docs/swift/latest/
var config = Realm.Configuration()
// Use the default directory, but replace the filename with the username
config.fileURL = config.fileURL!.deletingLastPathComponent()
.appendingPathComponent("\(username).realm")
Run Code Online (Sandbox Code Playgroud)
比
sUserRealm:Realm = try! Realm(configuration:config)
或任何你想要但不想要的路径Bundle。
创建和编辑文件和目录:
let fileManager:FileManager = FileManager.default
let documentdir:String = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last!
let dbName = someDataBaseFileName
let yourFullFileName = String(format:"%@/%@",documentdir,dbName)
Run Code Online (Sandbox Code Playgroud)
//或者
let yourFullPath:String = URL(fileURLWithPath: documentdir).appendingPathComponent(dbName).path
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5856 次 |
| 最近记录: |