在RealmSwift iOS 7 Swift 2中查找默认域文件时出现问题

在RealmSwift iOS 7 Swift 2中查找默认域文件时出现问题,ios,swift,realm,Ios,Swift,Realm,在Xcode 7中尝试将realm与Swift 2集成到项目中时,我遇到了一个主要问题。我正在尝试查找default.realm文件,以便通常可以在realm浏览器中看到DB 到目前为止,我已经浏览了互联网,并尝试了以下解决方案 解决方案选项1: 尝试将文件路径打印到控制台,出现无法解决的错误,使用以下打印命令 print(Realm().path) 然后 let realm = Realm(path: "/Users/me/Desktop/TestRealm.realm") 解决方案选项

在Xcode 7中尝试将realm与Swift 2集成到项目中时,我遇到了一个主要问题。我正在尝试查找default.realm文件,以便通常可以在realm浏览器中看到DB

到目前为止,我已经浏览了互联网,并尝试了以下解决方案

解决方案选项1:

尝试将文件路径打印到控制台,出现无法解决的错误,使用以下打印命令

print(Realm().path)
然后

let realm = Realm(path: "/Users/me/Desktop/TestRealm.realm")
解决方案选项2:

尝试暂停模拟器并将其放入LLDB控制台

po Realm.defaultPath

它返回

错误::1:1:错误:使用未解析的标识符“Realm” Realm.defaultPath

以下是创建领域对象的文件供参考

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

这个问题是您需要使用
试试关键字,因为
Realm()
可能引发错误:

print(try!Realm().path)

或在调试器中:


试试看!Realm().path

经过一点调整,我找到了答案。因此,我将发布对我有效的print命令。注:这在Swift 2.1.1中

干杯


-RB

我知道这是一个老问题,但我留下这个答案,以防万一对某人有用。这对我来说对Swift 2.2有效

print(Realm.Configuration.defaultConfiguration.fileURL!)
print(Realm.Configuration.defaultConfiguration.fileURL!)