Type ‘AllImage’不符合协议’Decodable’/’Encodable’。

huangapple go评论52阅读模式
英文:

Type 'AllImage' does not conform to protocol 'Decodable'/ 'Encodable'

问题

struct AllImage: Codable {
    let mediaReferenceID: String?
    let url: String?
    let title: String?
    let description: String?
    let icon: String?
    let tags: [TagInfo]?
    let footerSection: FooterSection?

    enum CodingKeys: String, CodingKey {
        case mediaReferenceID = "media_reference_id"
        case url, title, description, icon
        case ctaInfo = "cta_info"
        case footerSection = "footer_section"
    }
}
英文:
struct AllImage: Codable {
    let mediaReferenceID: String?
    let url: String?
    let title: String?
    let description: String?
    let icon: String?
    let tags: [TagInfo]?
    let footerSection: FooterSection?

    enum CodingKeys: String, CodingKey {
        case mediaReferenceID = "media_reference_id"
        case url, title, description, icon
        case ctaInfo = "cta_info"
        case footerSection = "footer_section"
    }
}

Here the TagInfo and FooterSection is Codable too with containing only string types.

Any solution to this problem?

答案1

得分: 1

CodingKeys应该反映出所有继承了Codable的属性,不能有额外的字段。所以你还应该添加标签并删除ctaInfo。

struct AllImage: Codable {
    let mediaReferenceID: String?
    let url: String?
    let title: String?
    let description: String?
    let icon: String?
    let tags: [TagInfo]?
    let footerSection: FooterSection?

    enum CodingKeys: String, CodingKey {
        case mediaReferenceID = "media_reference_id"
        case url, title, description, icon
        // Commented out this
        // case ctaInfo = "cta_info"
        // Add tags
        case tags = "tags"
        case footerSection = "footer_section"
    }
}

struct TagInfo : Codable {
    
}

struct FooterSection : Codable {
    
}
英文:

CodingKeys should reflect all properties of that type inherits Codable and cant have extra field. So you should also add tags and remove ctaInfo.

struct AllImage: Codable {
    let mediaReferenceID: String?
    let url: String?
    let title: String?
    let description: String?
    let icon: String?
    let tags: [TagInfo]?
    let footerSection: FooterSection?

    enum CodingKeys: String, CodingKey {
        case mediaReferenceID = "media_reference_id"
        case url, title, description, icon
        // Commented out this
        // case ctaInfo = "cta_info"
        // Add tags
        case tags = "tags"
        case footerSection = "footer_section"
    }
}


struct TagInfo : Codable {
    
}

struct FooterSection : Codable {
    
}


huangapple
  • 本文由 发表于 2023年6月12日 16:09:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定