我有一个自定义的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 –
请查看我的答案。 –