我有一個自定義的ObjectMapper類。 我想基於數據將元素映射到不同的對象類型。 我已經實現瞭如下的邏輯。但它不給我的價值,只有空。ObjectMapper條件映射整個元素
class FeedObject : Object, Mappable {
dynamic var post : HomeDataModel?
dynamic var friends : Friends?
required convenience init?(map: Map) {
self.init()
}
func mapping(map: Map) {
var Mtype = ""
Mtype <- map["type"]
print("TYPEEEEEE", Mtype)
if Mtype == "FRIENDS" {
friends <- map
}
else {
post <- map
}
}
}
我該如何實現這種映射?
樣品JSON -
{ "feed_objects": [ { "type": "NORMAL", "status": "none", "invited": false, "comment": "hello", "time": "00:12" }, { "type": "NORMAL", "status": "none", "invited": true, "comment": "How are you?", "time": "04:15" }, { "type": "FRIENDS", "display_text": "Your friends are here.", "count": 23 }, { "type": "NORMAL", "status": "verified", "invited": true, "comment": "great", "time": "09:32" }] }
你能分享一個JSON響應嗎? –
@anilkukdeja添加樣本json –
請查看我的答案。 –