jjlei
jjlei

Reputation: 109

Swift 4.1 Type of expression is ambiguous without context

I have tried a few suggestion from other stack overflow questions with similar issues but they didn't work. My issue only happened when I updated to the latest Xcode 9.3 from Xcode 9.2.

This line of code is giving me:

"Type of expression is ambiguous without more context"

data = try container.decodeIfPresent([Model].self, forKey: .data)

Below is how my class looks like:

class JSONResponse<Model>: Decodable
{
    public var data: [Model]?
    public var details: JSONResponseDetails?
    public var errors: [JSONError]?

private enum CodingKeys: String, CodingKey
{
    case details
    case data
    case errors
}

public required init(from decoder: Decoder) throws
{
    let container = try decoder.container(keyedBy: CodingKeys.self)
    details = try container.decodeIfPresent(JSONResponseDetails.self, forKey: .details)
    errors = try container.decodeIfPresent([JSONError].self, forKey: .errors)
    data = try container.decodeIfPresent([Model].self, forKey: .data)

    }
}

Upvotes: 0

Views: 592

Answers (1)

vadian
vadian

Reputation: 285069

The generic Model must conform to Decodable, too

class JSONResponse<Model : Decodable>: Decodable

Upvotes: 1

Related Questions