我已经尝试了一些来自其他具有类似问题的堆栈溢出问题的建议,但它们没有用。我的问题只发生在我从 Xcode 9.2 更新到最新的 Xcode 9.3 时。
这行代码给了我:
"Type of expression is ambiguous without more context"
data = try container.decodeIfPresent([Model].self, forKey: .data)
下面是我的类(class)的样子:
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)
}
}
最佳答案
通用模型
也必须符合Decodable
class JSONResponse<Model : Decodable>: Decodable
关于Swift 4.1 表达式类型在没有上下文的情况下不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49676705/