VivienG
VivienG

Reputation: 2180

Why I can't access an element of a class instance in Swift

I'm currently writing an iPhone application in Swift. I have a global class instance of my user database, like this:

var currentUser = UserDB()

class UserDB: Object {
dynamic var id:String = "0"

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

var userName: String?
var firstName: String?
var lastName: String?
}

Then I try to print my user info with a print in another class:

UserDB {
id = 56826e22971f34731a07ba09;
userName = aze;
firstName = (null);
lastName = (null);
}

But if I try to print a single value, it won't works:

print(currentUser.userName)

Will print:

nil

Do you have any idea why?

By the way, is it a good idea to deal with user info like this? I put these info inside a Realm database on exit, when going to background, or after account upgrade.

Upvotes: 1

Views: 101

Answers (1)

Cristik
Cristik

Reputation: 32894

You need to declare the properties as dynamic if you want Realm to map them. It seems that you did this only for the id property.

dynamic var userName: String?
dynamic var firstName: String?
dynamic var lastName: String?

Upvotes: 4

Related Questions