我有这个课程:
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?()
}
我在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)
这是输出:
vivieng
nil
为什么我无法打印testUser.FacebookAccount?.userName
?
顺便说一下,我可以访问UserDB
但FacebookDB
和TwitterDB
都没有的任何价值吗?
答案 0 :(得分:1)
最后我找到了如何解决这个问题。
我需要创建一个这样的值:
let facebookAccount = FacebookDB()
然后把我想要的东西放在里面:
facebookAccount.userName = user.userName
最后在课堂上复制课程:
testUser.FacebookAccount = facebookAccount
瞧,那现在就行了!