在RealmSwift iOS 7 Swift 2

时间:2016-01-06 21:09:29

标签: ios swift realm

在尝试使用Xcode 7中的Swift 2将项目集成到一个项目时,我遇到了一个重大问题。我正在尝试找到default.realm文件,以便我通常可以在Realm Browser中看到数据库。

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

How to find my realm file?

解决方案选项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

3 个答案:

答案 0 :(得分:1)

经过一番调整,我找到了答案。所以我将发布适合我的打印命令。 注意:这是在Swift 2.1.1中

print(Realm.Configuration.defaultConfiguration.path!)

干杯,

-RB

答案 1 :(得分:0)

此问题是您需要使用try!关键字,因为Realm()会引发错误:

print(try! Realm().path)

或在调试器中:

po try! Realm().path

答案 2 :(得分:0)

我知道这是一个老问题,但我留下这个答案,以防它对某人有用。这适用于Swift 2.2

print(Realm.Configuration.defaultConfiguration.fileURL!)