我正在初始化我的领域实例:
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))
但是,我得到了这个错误:
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
所有领域在他们关于创建多个领域的快速文档中都有this示例,甚至将其逐字复制也会引发相同的错误。如何创建和访问领域文件?
答案 0 :(得分:2)
Bundle.url(forResource:withExtension:)
返回指定包中现有文件的路径。您链接到的Realm文档部分使用了在应用包中包含Realm文件的示例。
由于您似乎尝试在特定路径上创建新 Realm文件,因此您应该计算该路径并将其设置为fileURL
Realm.Configuration
1}}。您通常不希望使用Bundle
API,因为应用程序包中的文件不可写。相反,您应该构建一个相对于您的应用程序文档或缓存目录的路径:
let documentDirectory = FileManager.default.url(for: .documentDirectory, in: .userDomainMask,
appropriateFor: nil, create: false)
let url = documentDirectory.appendingPathComponent("my-new-realm.realm")
答案 1 :(得分:2)
Swift 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
}()
}
自定义领域文件的名称和路径
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
}
}()
}
答案 2 :(得分:0)
使用
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")
比
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)
//或
let yourFullPath:String = URL(fileURLWithPath: documentdir).appendingPathComponent(dbName).path
答案 3 :(得分:0)
这是我的Realm配置类,我遇到了Realm的这个问题,但终于解决了
import Foundation
import RealmSwift
class RealmConfiguration {
static let insensitiveDataEncryptionKey = "12345hamedhamedhamedhamedhamedhamedhamedhamedhamedhamedhamedhame"
static var sensitiveDataEncryptionKey = "hamedhamedhamedhamedhamedhamedhamedhamedhamedhamedhamedhamedhame"
public static var defaultDataConfiguration = Realm.Configuration.defaultConfiguration
static func insensitiveDataConfiguration() -> Realm.Configuration {
var config = Realm.Configuration()
config.fileURL = config.fileURL!.deletingLastPathComponent().appendingPathComponent("insensitive.realm")
config.encryptionKey = insensitiveDataEncryptionKey.data(using: String.Encoding.utf8, allowLossyConversion: false)
return config
}
static func sensitiveDataConfiguration() -> Realm.Configuration {
var config = Realm.Configuration()
config.fileURL = config.fileURL!.deletingLastPathComponent().appendingPathComponent("sensitive.realm")
config.encryptionKey = sensitiveDataEncryptionKey.data(using: String.Encoding.utf8, allowLossyConversion: false)
return config
}
}