Please learn to understand the decoding error messages, they are very descriptive.
The error says you are going to decode an array but the actual object is a dictionary (the target struct).
First take a look at the beginning of the JSON
{
"copyright" : "NHL and the NHL Shield are registered trademarks of the National Hockey League. NHL and NHL team marks are the property of the NHL and its teams. ? NHL 2018. All Rights Reserved.",
"totalItems" : 2,
"totalEvents" : 0,
"totalGames" : 2,
"totalMatches" : 0,
"wait" : 10,
"dates" : [ {
"date" : "2018-05-04",
It starts with a {
which is a dictionary (an array is [
) but you want to decode an array ([Dates]
), that's the type mismatch the error message is referring to.
But this is only half the solution. After changing the line to try decoder.decode(Dates.self
you will get another error that there is no value for key copyright
.
Look again at the JSON and compare the keys with the struct members. The struct whose members match the JSON keys is Initial
and you have to get the dates
array to populate gameData
.
let jsondata = try decoder.decode(Initial.self, from: detailData)
gameData = jsondata.dates
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…