Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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
Ios 应为数组,但在Swift中找到字典_Ios_Swift_Codable_Nsurlrequest - Fatal编程技术网

Ios 应为数组,但在Swift中找到字典

Ios 应为数组,但在Swift中找到字典,ios,swift,codable,nsurlrequest,Ios,Swift,Codable,Nsurlrequest,我对Swift中的Codable还不熟悉,我正试图从API()中获取一些数据。我不需要从API获取所有数据,只需要在模型中指定字段。我在尝试运行代码时遇到此错误:typeMismatch(Swift.Array,Swift.DecodingError.Context(codingPath:[],debugDescription:“应解码数组,但找到了字典。”,underlineerror:nil)) 奇怪的是,当我把API响应放在一个字符串中,然后对其进行编码时,我可以用同样的精确代码成功地对其

我对Swift中的Codable还不熟悉,我正试图从API()中获取一些数据。我不需要从API获取所有数据,只需要在模型中指定字段。我在尝试运行代码时遇到此错误:
typeMismatch(Swift.Array,Swift.DecodingError.Context(codingPath:[],debugDescription:“应解码数组,但找到了字典。”,underlineerror:nil))

奇怪的是,当我把API响应放在一个字符串中,然后对其进行编码时,我可以用同样的精确代码成功地对其进行解码。有人有什么见解吗?我从回复中得到一个
200
代码

代码:

API返回结构(来自链接):

以下是我的模型:

public struct Game: Codable {
    public var gameId: String?
    public var details: Details?
    public var teams: Teams?
    public var scoreboard: Scoreboard?
}

public struct Details: Codable {
    public var league: String?
    
}

public struct Teams: Codable {
    public var home: Team?
    public var away: Team?
}

public struct Team: Codable {
    public var team: String?
    public var abbreviation: String?
}

public struct Scoreboard: Codable {
    public var score: Score?
    public var currentPeriod: Int?
    public var periodTimeRemaining: String?
}

public struct Score: Codable {
    public var away: Int?
    public var home: Int?
}

让我们看看api的响应:

    "status": 200,
    "time": "2021-03-26T01:42:14.010Z",
    "games": 5,
    "skip": 0,
    "results": [....]
响应是Dictionary,但您将其编码到[Game]的数组中,说明您收到此错误的原因。你需要创建更多这样的模型

public struct GameResponse: Codable {
    var status: Int
    var time: String
    var games: Int
    var skip: Int
    var results: [Game]

}
并解码对GameSponseModel的响应:

    let games = try dec.decode(GameResponse.self, from: data)

并将游戏id更改为Int,因为它不是字符串

public struct Game: Codable {
    public var gameId: Int?
    public var details: Details?
    public var teams: Teams?
    public var scoreboard: Scoreboard?
}

让我们看看api的响应:

    "status": 200,
    "time": "2021-03-26T01:42:14.010Z",
    "games": 5,
    "skip": 0,
    "results": [....]
响应是Dictionary,但您将其编码到[Game]的数组中,说明您收到此错误的原因。你需要创建更多这样的模型

public struct GameResponse: Codable {
    var status: Int
    var time: String
    var games: Int
    var skip: Int
    var results: [Game]

}
并解码对GameSponseModel的响应:

    let games = try dec.decode(GameResponse.self, from: data)

并将游戏id更改为Int,因为它不是字符串

public struct Game: Codable {
    public var gameId: Int?
    public var details: Details?
    public var teams: Teams?
    public var scoreboard: Scoreboard?
}

你的代码对我来说是有效的。问题是它与实际的API不兼容,那么请帮助我们了解它的工作方式与你不一样。如果我们不能重现这个问题,我们也帮不了你。你的代码对我有效。问题是它与实际的API不起作用,那么请帮助我们看到它的工作方式与你不一样。如果我们无法重现问题,我们将无能为力。从哪里得到响应?我使用您的rapidapi密钥请求用户邮递员好的,谢谢您的回复reply@NoahIarrobino没什么,快乐编码:)@NoahIarrobino总是和邮递员一起测试反应,您应该使用generic创建BaseModel以节省时间从何处获得响应?I用户邮递员请求和您的rapidapi密钥好的,谢谢您的帮助reply@NoahIarrobino没什么,快乐编码:)@NoahIarrobino总是用postman测试响应,你应该用generic创建BaseModel来节省时间