【发布时间】:2017-03-08 12:29:02
【问题描述】:
我有一个自定义的 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
-
请查看我的回答。
标签: ios swift swift3 objectmapper