在我的领域对象模型中,我有一个名为“事件”的对象。每个 Event 都有一个 EventLocatons 列表。我正在尝试从 json 映射这些对象,但 EventLocations 列表始终为空。对象看起来像这样(为清楚起见进行了简化):
class Event: Object, Mappable {
override class func primaryKey() -> String? {
return "id"
}
dynamic var id = ""
var eventLocations:List<EventLocation> = List<EventLocation>()
func mapping(map: Map) {
id <- map["id"]
eventLocations <- map["eventLocations"]
}
}
class EventLocation: Object, Mappable {
override class func primaryKey() -> String? {
return "id"
}
dynamic var id: String = ""
dynamic var name: String = ""
required convenience init?(_ map: Map) {
self.init()
}
func mapping(map: Map) {
id <- map["id"]
name <- map["name"]
}
}
我拥有的 json 是一个 Event 对象数组。它来自 Alamofire 响应,我将其映射为:
var events = Mapper<Event>().mapArray(json!)
json 看起来像这样:
[
{
"id" : "21dedd6d",
"eventLocations" : [
{
"name" : "hh",
"id" : "e18df48a",
},
{
"name" : "tt",
"fileId" : "be6116e",
}
]
},
{
"id" : "e694c885",
"eventLocations" : [
{
"name" : "hh",
"id" : "e18df48a",
},
{
"name" : "tt",
"fileId" : "be6116e",
}
]
}
]
有谁知道如何使用 Mappable 协议映射自定义对象列表。为什么“eventLocations”列表总是空的?