Swift 无法强制转换类型为'__NSYi';至';NSData&x27;
我正在尝试解码以下数据:Swift 无法强制转换类型为'__NSYi';至';NSData&x27;,swift,alamofire,jsondecoder,Swift,Alamofire,Jsondecoder,我正在尝试解码以下数据: AF.upload(postData!, to: loginUrlString, headers: postmanManager.headers).responseJSON { response in switch response.result { case .success(let value): print(value) do { let results
AF.upload(postData!, to: loginUrlString, headers: postmanManager.headers).responseJSON { response in
switch response.result {
case .success(let value):
print(value)
do {
let results = try JSONDecoder().decode(User.self, from: value as! Data)
DispatchQueue.main.async {
let id = results.Usuario[0].Id
let name = results.Usuario[0].Name
let userInfo = UserModel(Id: id, firstName: name)
print(userInfo)
}
} catch {
print(error)
}
case .failure(let error):
print(error)
}
}
“用户”对象属于以下结构:
struct User: Codable {
let Usuario: [UserData]
}
struct UserData: Codable {
let Id: Int
let Name: String
}
我试着查找答案,根据我的理解,我试着将值更改为!数据
到值为![String:Any]
,但我遇到另一个错误:
无法将“[String:Any]”类型的值转换为预期的参数类型“Data”
我还能试着换什么?谢谢你的帮助 您使用了错误的响应类型 替换
.responseJSON
与
请以小写字母开头命名函数和变量
.responseData