{
"userName": "Jack",
"id": 10086,
"userID": 123,
"content": "This is an album content!"
"userAvatar": "https://useravatarurl.png"
}
The userNameuserIDuserAvatar are for the owner of the album, which are in UserModel.
The idcontent are for album.
Your model:
The model is RealmSwift Object Model:
class UserModel: Object, Mappable {
var id = 0
var userName: String = ""
var avatarUrl: String?
init(_ map: Map) {
id <- map["id"]
avatarUrl <- map["avatar"]
username <- map["userName"]
}
}
class AlbumModel: Object, Mappable {
var id = 0
var content: String = ""
var owner: UserModel?
init(_ map: Map) {
id <- map["id"]
content <- map["content"]
// owner <- map["id"] ?? How could I map userID, userName, userAvatar into a realm object?
}
}
I think I have got my answer.
What I need is to map the json twice, one for AlbumModel, and the other for UserModel.
In AlbumModel, I map the userID in json into UserModel-type property owner.
Your JSON dictionary:
The
userName
userID
userAvatar
are for the owner of the album, which are inUserModel
. Theid
content
are for album.Your model:
The model is RealmSwift Object Model:
Thanks for any help.