Swift 4.1 表达式类型在没有上下文的情况下不明确

标签 swift swift4

我已经尝试了一些来自其他具有类似问题的堆栈溢出问题的建议,但它们没有用。我的问题只发生在我从 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/

相关文章:

ios - 如何在没有动画的情况下更新 SwiftUI 列表

ios - MGLPolyLine 颜色更改不起作用 - Swift 4、Mapbox

swift - 对 SpriteKit 节点进行分组,以及 zPosition 控制

ios - Swift 4 尝试呈现其 View 不在窗口层次结构中的 ViewController

dictionary - 从 userInfo 字典中获取字符串

swift - UIPickerView 无需 segue 发送数据

ios - 如何在结构模型中访问枚举? swift

ios - UIScrollView 的 subview 保持在同一位置,而 UIScrollView 垂直滚动

ios - 如何更改分组的 uitableview 的标题字体?

swift - 如何使用子类来遵守协议(protocol)