首页 > 解决方案 > 无法使用“([[Notice]?])”类型的参数列表调用“解码”

问题描述

当我确认可解码协议时,如何快速从解码器解码列表

struct ActiveModuleRespones:Codable {
    var Notice:[Notice]?
    var Module:[Module]?


    public init(from: Decoder) throws {
        //decoding here
        let container = try from.singleValueContainer()
        self.Notice = try? container.decode([Notice].self)
    }

}

收到此错误:

Cannot invoke 'decode' with an argument list of type '([[Notice]?])'

截屏: 在此处输入图像描述

请帮忙 ,

标签: swiftcodabledecodable

解决方案


它与变量本身混淆。更改变量的名称以修复它。

struct ActiveModuleRespones: Codable {
    var notice: [Notice]?
    var module: [Module]?

    public init(from: Decoder) throws {
        //decoding here
        let container = try from.singleValueContainer()
        self.notice = try? container.decode([Notice].self)
    }
}

在 Swift 中,所有类型都有UpperCamelCase名称,几乎所有其他类型都有lowerCamelCase名称。

最后,使用try?会杀死所有异常,你永远不会知道出了什么问题,请尝试使用它:

self.notice = try container.decode([Notice]?.self)

推荐阅读