Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SWift 4 JSONDecoder从Alamofire响应解码_Swift_Alamofire_Swift4_Jsondecoder - Fatal编程技术网

SWift 4 JSONDecoder从Alamofire响应解码

SWift 4 JSONDecoder从Alamofire响应解码,swift,alamofire,swift4,jsondecoder,Swift,Alamofire,Swift4,Jsondecoder,我有一个包含其他结构和数组的结构 public struct Report: Codable { let s:Student; let vio:[VIO]; let stuPresence: [StuPresence]; } 我正在尝试新的jsondeconder()将alamofire响应转换为我的结构 sessionManager.request( self.url_report+"?d="+date, method: .get, parameters: nil).respons

我有一个包含其他结构和数组的结构

public struct Report: Codable {
 let s:Student;
 let vio:[VIO];
 let  stuPresence: [StuPresence]; 
}
我正在尝试新的
jsondeconder()
将alamofire响应转换为我的结构

sessionManager.request( self.url_report+"?d="+date, method: .get, parameters: nil).responseJSON{ response in
    if response.response?.statusCode == 200 {
            debugPrint(response)
            do{
                let r = try JSONDecoder().decode(Report.self, from: response.result.value as! Data)
                debugPrint(r);
            }catch{
               self.showMessage(message: self.general_err)
            }
    }
}
问题是,在我的
报告
结构中解码后,我得到的不是字符串,而是数字(从调试模式检查)。我做错了什么

更新:它也给出了错误

Could not cast value of type '__NSDictionaryI' (0x108011508) to 'NSData' (0x108010090)

错误很明显:

response.result.value
显然是一个字典(
\uu NSDictionaryI
),它不能转换为
(NS)数据。这意味着JSON已经被反序列化


为了能够使用
JSONDecoder
您必须更改
Alamofire
设置以返回原始
数据

错误非常明显:

response.result.value
显然是一个字典(
\uu NSDictionaryI
),它不能转换为
(NS)数据。这意味着JSON已经被反序列化


为了能够使用
JSONDecoder
您必须更改
Alamofire
设置以返回原始
数据

response.Data而不是response.result.value在这些更改之后,我最终进入了
catch
块问题可能出在模型中,你能在问题中给出一个子样本和模型结构吗?response.data而不是response.result.value在这些更改之后,我最终进入了
catch
区块问题可能出在模型中,你能在问题中给出一个子样本和模型结构吗?对如何返回原始数据有什么建议吗?我尝试了许多变体,结果总是出现在
catch
块中。按照注释(
response.data
)中的建议,您会遇到什么错误?您甚至不处理实际的错误。如果您最终进入
catch
块,则会出现
错误
<代码>打印
它<代码>自身。一般错误是没有意义的。永远不要忽略
catch
块中传递的
error
s。
错误信息:valueNotFound(Swift.Int,Swift.DecodingError.Context(codingPath:[CodingKeys(stringValue:“s”,intValue:nil),CodingKeys(stringValue:“id”,intValue:nil)],debugDescription:“应为Int值,但却发现为null。”,underyingerror:nil))
使用编码键并将其保留在外。为属性指定一个默认值或使其成为可选值。对如何返回原始数据有何建议?我尝试了许多变体,结果总是出现在
catch
块中。按照注释(
response.data
)中的建议,您会遇到什么错误?您甚至不处理实际的错误。如果您最终进入
catch
块,则会出现
错误
<代码>打印
它<代码>自身。一般错误是没有意义的。永远不要忽略
catch
块中传递的
error
s。
错误信息:valueNotFound(Swift.Int,Swift.DecodingError.Context(codingPath:[CodingKeys(stringValue:“s”,intValue:nil),CodingKeys(stringValue:“id”,intValue:nil)],debugDescription:“应为Int值,但却发现为null。”,underyingerror:nil))
使用编码键并将其保留在外。为属性指定默认值或使其成为可选值。