Alamofire responseJSON无法将NSDictionary类型的值转换为自定义对象

时间:2016-12-28 09:57:59

标签: ios json swift alamofire

Alamofire.request(todoEndpoint)
        .responseJSON { response in
            switch response.result {
            case .success(let JSON):
                let response: ETOOperatorsList = JSON as! ETOOperatorsList
                delegate.requestWithReference(requestReference: "112", model: response as AnyObject)
            case .failure(let error):
                delegate.requestWithReference(requestReference: "112", error: error)
        }
    }

无法投放错误

  

“无法将'__NSDictionaryI'类型的值强制转换为'OperatorsList'。”

如何将Alamofire responseJSON强制转换为自定义对象?

谢谢!

1 个答案:

答案 0 :(得分:0)

Apple没有提供OJM(Object / Json Mapper)数据绑定功能,将PONSO转换为JSON,反之亦然。您可以进行手动映射,也可以使用某些第三方映射。例如

https://github.com/Hearst-DD/ObjectMapper https://github.com/tristanhimmelman/AlamofireObjectMapper