在以UTF-8格式转换响应时遇到问题

时间:2019-05-29 06:24:12

标签: ios swift encoding decode

此行出现错误:

Data(from: responseValue.utf8 as! Decoder)

enter image description here

  

无法将“数据”类型的值转换为预期的参数类型“数据”

do{
    let responseData = try JSONDecoder().decode(forget_Base.self, from: Data(from: responseValue.utf8 as! Decoder))
    if responseData.code == 200
    {
        let message = isNilValue(assignValue: responseData.message)
        let alert = UIAlertController(title: "", message: message, preferredStyle: .alert)
        let action = UIAlertAction(title: "OK", style: .default, handler: {(_action) -> Void in
            let next = self.storyboard?.instantiateViewController(withIdentifier: "SetPasswordVCSID")
            self.navigationController?.pushViewController(next!, animated: true)
        })
        alert.addAction(action)
        self.present(alert, animated: true, completion: nil)
    }
    else
    {
    }
}
catch
{
    print("error")
}

我该如何解决?我已经尝试了太多东西,但仍然坚持下去。

2 个答案:

答案 0 :(得分:1)

根据错误消息,很简单

let responseData = try JSONDecoder().decode(forget_Base.self, from: Data(responseValue.utf8))

强制转换为Decoder的类型毫无意义。

请遵守命名约定,forget_Base很可怕,将其命名为ForgetBase

答案 1 :(得分:0)

照原样使用responseValue。 这是代码: 更改以下内容

let responseData = try JSONDecoder().decode(forget_Base.self, from: Data(from: responseValue.utf8 as! Decoder))

let responseData = try JSONDecoder().decode(forget_Base.self, from:responseValue)