Reputation: 2180
I have this classes:
class FacebookDB: Object {
dynamic var userName: String = ""
dynamic var profilePicture: String = ""
}
class TwitterDB: Object {
dynamic var userName: String = ""
dynamic var profilePicture: String = ""
}
class UserDB: Object {
dynamic var id = 0
override class func primaryKey() -> String {
return "id"
}
dynamic var userName: String = ""
dynamic var firstName: String = ""
dynamic var lastName: String = ""
dynamic var email: String = ""
dynamic var facebookAccount = FacebookDB?()
dynamic var twitterAccount = TwitterDB?()
}
After I put a value inside the userName
//user value come from facebook
let testUser = UserDB()
let realm = try! Realm()
testUser.FacebookAccount?.userName = user.userName
print(user.userName)
print(testUser.FacebookAccount?.userName)
Here is the output:
vivieng
nil
Why can't I print testUser.FacebookAccount?.userName
?
BTW I can access every value from UserDB
but none of FacebookDB
nor TwitterDB
, any idea?
Upvotes: 1
Views: 25
Reputation: 2180
Finally I found how to deal with this.
I need to create a value like that:
let facebookAccount = FacebookDB()
And then put everything I want inside:
facebookAccount.userName = user.userName
And finally copy the class inside the class:
testUser.FacebookAccount = facebookAccount
And voilà, that's work now!
Upvotes: 1