No value associated with key in URLRequest Swift

138 Views Asked by At

I am trying to create URLRequest to an API that has a logical OR statement for a parameter, but cannot get it to work. It will work when status=final or status=in progress but not status=final||in progress. I keep getting this error when trying the logical OR : keyNotFound(CodingKeys(stringValue: "scoreboard", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "results", intValue: nil), _JSONKey(stringValue: "Index 2", intValue: 2)], debugDescription: "No value associated with key CodingKeys(stringValue: \"scoreboard\", intValue: nil) (\"scoreboard\").", underlyingError: nil))

this is my code:

var components = URLComponents()
components.scheme = "https"
components.host = "sportspage-feeds.p.rapidapi.com"
components.path = "/games"
components.queryItems = [(URLQueryItem(name:"status", value:"in progress||status=final")), ]

guard let url = components.url else {return}
let request = NSMutableURLRequest(url: url, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)
request.httpMethod = "GET"
request.allHTTPHeaderFields = headers
        
        let session = URLSession.shared
        session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
            
            if let data = data {
                
                do {
                    let dec = JSONDecoder()
                    dec.keyDecodingStrategy = .convertFromSnakeCase
                    let games = try dec.decode(LSGameResponse.self, from: data)
                    
                } catch {
                    print(error)
                }
   
            }
        }).resume()

Here is my model:

public struct LSGameResponse: Codable {
    var status: Int?
    var games: Int?
    var results: [LSGame]?
}

public struct LSGame: Codable {
    public var gameId: Int
    public var details: LSDetails
    public var teams: LSTeams
    public var scoreboard: LSScoreboard
    public var status: String
}

public struct LSDetails: Codable {
    public var league: String
    
}

public struct LSTeams: Codable {
    public var home: LSTeam
    public var away: LSTeam
}

public struct LSTeam: Codable {
    public var team: String
    public var abbreviation: String
}

public struct LSScoreboard: Codable {
    public var score: LSScore
    public var currentPeriod: Int
    public var periodTimeRemaining: String?
}

public struct LSScore: Codable {
    public var away: Int
    public var home: Int
}
0

There are 0 best solutions below