在RealmSwift iOS 7 Swift 2中发现默认域文件

时间:2023-01-22 15:15:47

I am having a major issue when trying to integrate realm into a project with Swift 2 in Xcode 7. I am trying to find the default.realm file so that I can usually see the DB in Realm Browser.

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

So far I have scoured the internet and tried the following solutions...

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

How to find my realm file?

如何找到我的领域文件?

Solution option 1:

解决方案选项1:

Tried printing the file path to the console, comes up with errors that I cannot solve, used the following print commands

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

print(Realm().path)

and then...

let realm = Realm(path: "/Users/me/Desktop/TestRealm.realm")

Solution option 2:

解决方案选项2:

Tried pausing the simulator and putting this into the LLDB console...

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

po Realm.defaultPath

which returns...

error: :1:1: error: use of unresolved identifier 'Realm' Realm.defaultPath

错误:: 1:1:错误:使用未解析的标识符'Realm'Ereal.DefaultPath

For reference here are the files that create the realm objects

这里参考的是创建领域对象的文件

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
            }

        }
    }
}

If anyone has any guidance about where to find a solution to this problem that would be wonderful. Thanks!

如果有人有任何关于在哪里找到这个问题的解决方案的指导,这将是非常好的。谢谢!

-RB

3 个解决方案

#1


1  

After a little tweaking, I was able to find the answer. So I will post the print command that worked for me. Note: this is in Swift 2.1.1

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

print(Realm.Configuration.defaultConfiguration.path!)

Cheers,

-RB

#2


0  

This issue is that you need to use the try! keyword since Realm() can throw an error:

这个问题是你需要使用试试!关键字,因为Realm()可以抛出错误:

print(try! Realm().path)

or in the debugger:

或者在调试器中:

po try! Realm().path

宝试试!境界()。路径

#3


0  

I know is an old question but I leave this answer just in case it would be useful for somebody. This worked for me with Swift 2.2

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

print(Realm.Configuration.defaultConfiguration.fileURL!)

#1


1  

After a little tweaking, I was able to find the answer. So I will post the print command that worked for me. Note: this is in Swift 2.1.1

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

print(Realm.Configuration.defaultConfiguration.path!)

Cheers,

-RB

#2


0  

This issue is that you need to use the try! keyword since Realm() can throw an error:

这个问题是你需要使用试试!关键字,因为Realm()可以抛出错误:

print(try! Realm().path)

or in the debugger:

或者在调试器中:

po try! Realm().path

宝试试!境界()。路径

#3


0  

I know is an old question but I leave this answer just in case it would be useful for somebody. This worked for me with Swift 2.2

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

print(Realm.Configuration.defaultConfiguration.fileURL!)