Swift 无法调用';解码';具有类型为';([[通知]?])和#x27;

Swift 无法调用';解码';具有类型为';([[通知]?])和#x27;,swift,codable,decodable,Swift,Codable,Decodable,当我确认可解码协议时,如何在swift中从解码器解码列表 struct ActiveModuleRespones:Codable { var Notice:[Notice]? var Module:[Module]? public init(from: Decoder) throws { //decoding here let container = try from.singleValueContainer() sel

当我确认可解码协议时,如何在swift中从解码器解码列表

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]?])'
截图:


请帮助,

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

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中,所有类型都有大写的名称,其他类型几乎都有小写的名称

最后,使用
try?
将消除所有异常,您永远不会知道是什么错误,请尝试使用以下方法:

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

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

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中,所有类型都有大写的名称,其他类型几乎都有小写的名称

最后,使用
try?
将消除所有异常,您永远不会知道是什么错误,请尝试使用以下方法:

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

如果需要可选值,请与非可选类型一起使用,而不使用
尝试?

struct ActiveModuleRespones: Decodable {
    var notice: [Notice]?

    enum CodingKeys: String, CodingKey {
       case notice
    }

    public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.notice = try container.decodeIfPresent([Notice].self, forKey: .notice)
    }
}

如果需要可选值,请与非可选类型一起使用,而不使用
尝试?

struct ActiveModuleRespones: Decodable {
    var notice: [Notice]?

    enum CodingKeys: String, CodingKey {
       case notice
    }

    public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.notice = try container.decodeIfPresent([Notice].self, forKey: .notice)
    }
}

第一个规则-类、结构和枚举名称以大写字母开头。变量、方法和大小写名称以小写字母开头。第二条规则-变量名称不能与类型名称完全相同。太多的混乱。解决这些问题,你的代码就会更好。天哪!我遵循这条规则,但仅在这个类中,我们得到了
注意
模块
我将尝试使用自定义键(CodingKeys),非常感谢您对@rmaddyFirst规则的评论-类、结构和枚举名称以大写字母开头。变量、方法和大小写名称以小写字母开头。第二条规则-变量名称不能与类型名称完全相同。太多的混乱。解决这些问题,你的代码就会更好。天哪!我遵循这一规则,但仅在本课程中,我们会收到
通知
模块
我将尝试使用自定义密钥(CodingKeys),非常感谢您对@rmaddy的评论