我正在使用 Alamofire、Objectmapper、Realm,一切都在做一件事情:我无法映射嵌套对象。
class Voting: Object, Mappable {
dynamic var votingID: String = ""
dynamic var question: String = ""
var votingOptions = List<VotingOption>()
required convenience init?(_ map: Map) {
self.init()
}
func mapping(map: Map) {
votingID <- map["id"]
question <- map["question"]
votingOptions <- map["votingOptions"]
}
override class func primaryKey() -> String {
return "votingID"
}
}
class VotingOption: Object, Mappable{
dynamic var optionID: String = ""
dynamic var text: String = ""
required convenience init?(_ map: Map) {
self.init()
}
func mapping(map: Map) {
optionID <- map["id"]
text <- map["optionText"]
}
override class func primaryKey() -> String {
return "optionID"
}
}
我要映射的 JSON 是:
{
"Voting": [
{
"question": "Which option do yo prefer?",
"id": "7f073efd-6f3d-43f2-9fe4-5cad683b77a2",
"votingOptions": [
{
"optionText": "Option 3",
"id": "3bc0a618-8791-4862-a7fd-5f2df464697d"
},
{
"optionText": "Option 1",
"id": "84c6a830-814b-40c8-a252-c074be5d689a"
},
{
"optionText": "Option 2",
"id": "8872ef6f-fc70-445a-802e-d39944006467"
}
]
}
]
}
VotingOption 中的映射函数永远不会被调用。