我正在尝试实现 Swift 4 的新 Decodable 协议并且很难接近它。
这是我的 JSON 服务器响应:
{
"success": true,
"errorCode": 0,
"message": "Succcess",
"data": {
"name": "Logan Howlett",
"nickname": "The Wolverine",
"image": "http://heroapps.co.il/employee-tests/ios/logan.jpg",
"dateOfBirth": 1880,
"powers": [
"Adamantium Bones",
"Self-Healing",
"Adamantium Claws"
],
"actorName": "Hugh Jackman",
"movies": [
{
"name": "X-Men Origins: Wolverine",
"year": 2009
},
{
"name": "The Wolverine",
"year": 2013
},
{
"name": "X-Men: Days of Future Past",
"year": 2014
},
{
"name": "Logan",
"year": 2017
},
]
}
}
data
解码响应部分的最佳方法是什么?另外,如果data
突然是一个array
而不是一个对象会发生什么,我怎样才能支持这两种数据类型?
非常感谢 :)