Cloud9999Strife
Cloud9999Strife

Reputation: 3202

Type casting an RLMObject throws away data

I have code that saves an RLMObject subclass to the realm database. This code works and I have used the realm browser to verify that it is saved as expected.

I then want to query the realm database for this object that I saved, and I want to cast it to the RLMObject subclass that is was before I saved it.

Here is the code:

let queryResults = RealmSubclass.allObjects()

for result in queryResults {
    if result is RealmSubclass {
        let temp = result as RealmSubclass
        println(temp.name)
        println(temp.dateOfBirth)
        println(temp.gender)
    }
}

When I check the values in the debug console, using print object, I see values that I expect. However, when I do a type cast to RealmSubclass the resulting object has no correct values, only nil values.

Why could this be? I have read the documentation, to no avail.

EDIT: Here is the RLMObject subclass:

public class RealmSubclass: RLMObject {
    public dynamic var id: String = NSUUID().UUIDString
    public dynamic var name: String = ""
    public dynamic var dateOfBirth: NSDate = NSDate()
    public dynamic var gender: NSString = Consts.Gender.Male

    override public class func primaryKey() -> String {
        return "id"
    }
}

Upvotes: 1

Views: 157

Answers (1)

Cloud9999Strife
Cloud9999Strife

Reputation: 3202

Ok, it seems that the values were actually being returned. What happened is that Swift debugging is not up to standard yet. The debug area was showing incorrect information.

Upvotes: 1

Related Questions