Q:

realm objectForPrimaryKey can not found my object in database

I define a class named User And set it's primaryKey 'userId' like this:
class User: Object {

    var userId = 0
    var name = "haha"

    override static func primaryKey() -> String? {
        return "userId"
    }    
}

then I write some data to defualt database, and I can find it with realm browser:
let user = User()
user.userId = 345
user.name = "哈哈"
print("\n\n" + NSHomeDirectory())

try! realm.write { 
    realm.add(user, update: true)
}

but in the end I use
let user = realm.objectForPrimaryKey(User.self, key: 345)

it got a object with userId = 0 and name = "haha"
ps: my english is not good,please use chinese if you can,thx

A:

Not sure if this helps but i remember that we need to add dynamic before var like this:
class User: Object {

dynamic var userId = 0
dynamic var name = "haha"

    override static func primaryKey() -> String? {
        return "userId"
    }    
}

