当我尝试在 Xcode 7 中使用 Swift 2 将 Realm 集成到项目中时遇到一个主要问题。我试图找到 default.realm 文件,以便我通常可以在 Realm 浏览器中看到数据库。
到目前为止,我已经搜索了互联网并尝试了以下解决方案...
解决方案一:
尝试将文件路径打印到控制台,出现我无法解决的错误,使用了以下打印命令
print(Realm().path)
然后……
let realm = Realm(path: "/Users/me/Desktop/TestRealm.realm")
方案二:
尝试暂停模拟器并将其放入 LLDB 控制台...
po Realm.defaultPath
返回...
error: :1:1: error: use of unresolved identifier 'Realm' Realm.defaultPath
此处供引用的是创建 Realm 对象的文件
import UIKit
import RealmSwift
class XMCMovie: Object {
dynamic var id = ""
dynamic var title = ""
dynamic var tomatometer = 0
dynamic var consensus = ""
dynamic var imageName = ""
override class func primaryKey() -> String? {
return "id"
}
required init() {
super.init()
}
init(id: NSString, title: NSString, tomatometer: Int, consensus: NSString, imageName: NSString) {
super.init()
self.id = id as String
self.title = title as String
self.tomatometer = tomatometer
self.consensus = consensus as String
self.imageName = imageName as String
}
}
import UIKit
import RealmSwift
class XMCApi {
class func requestOpeningMovies() {
let movies = [ XMCMovie(id: "0", title: "The Hobbit: The Battle Of The Five Armies", tomatometer: 62, consensus: "Suitably grim, epic, and action-packed, The Hobbit: The Battle of the Five Armies ends Peter Jackson's second Middle-earth trilogy on a rousing high note.", imageName: "hobbit"),
XMCMovie(id: "1", title: "Night At The Museum: Secret Of The Tomb", tomatometer: 53, consensus: "No consensus yet.", imageName: "museum"),
XMCMovie(id: "2", title: "Annie", tomatometer: 20, consensus: "The new-look Annie hints at a progressive take on a well-worn story, but smothers its likable cast under clichés, cloying cuteness, and a distasteful materialism.", imageName: "annie"),
XMCMovie(id: "3", title: "Mr. Turner", tomatometer: 97, consensus: "Led by a masterful performance from Timothy Spall and brilliantly directed by Mike Leigh, Mr. Turner is a superior Hollywood biopic.", imageName: "turner"),
XMCMovie(id: "4", title: "Song Of The Sea", tomatometer: 100, consensus: "No consensus yet.", imageName: "sea") ]
// Write our movie objects to the database
let realm = try! Realm()
try! realm.write() {
for movie in movies {
/* This method will avoid duplicating records by looking at the
primary key we've set on our object. Go look at the XMCMovie
class to see that method defined.
*/
// XMCMovie.createOrUpdateInDefaultRealmWithObject(movie)
// Alternatively, you could add new objects by calling this method
realm.add(movie)
// or
// realm.addObjects(movies) // An array of objects
}
}
}
}
如果有人对在哪里可以找到这个问题的解决方案有任何指导,那就太好了。谢谢!
-RB
最佳答案
经过一些调整,我找到了答案。所以我将发布对我有用的打印命令。 注意:这是在 Swift 2.1.1 中
print(Realm.Configuration.defaultConfiguration.path!)
干杯,
-RB
关于ios - 在 RealmSwift iOS 7 Swift 2 中查找默认 Realm 文件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34642785/