我无法将Alamofire查询中的json响应解析为模型。我有这个型号代码。我究竟做错了什么?我在Xcode 8.3中使用Swift 3
enum SerializationError: Error {
case missing(String)
case invalid(String, Any)
}
struct Thing {
var id: String
var name: String
}
extension Thing {
init(json: [String: Any]) throws {
guard let id = json["id"] as? String else {
throw SerializationError.missing("id")
}
guard let name = json["name"] as? String else {
throw SerializationError.missing("name")
}
self.id = id
self.name = name
}
}
然后在我的控制器中我有
func parseData(jsonData: [String: Any]) {
var model = [Thing]()
let things = jsonData["things"] as! [[String: Any]]
for thing in things {
do {
let aThing = try Thing(json: thing)
model.append(aThing)
} catch let error {
print(error.localizedDescription)
}
}
}
我总是收到错误。我知道错误不是关于json响应,因为我仔细检查了它,并在那里有额外的代码来测试元素是否存在。
The operation couldn’t be completed. (MyApp.SerializationError error 0.)