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

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

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

问题

  1. struct AllImage: Codable {
  2. let mediaReferenceID: String?
  3. let url: String?
  4. let title: String?
  5. let description: String?
  6. let icon: String?
  7. let tags: [TagInfo]?
  8. let footerSection: FooterSection?
  9. enum CodingKeys: String, CodingKey {
  10. case mediaReferenceID = "media_reference_id"
  11. case url, title, description, icon
  12. case ctaInfo = "cta_info"
  13. case footerSection = "footer_section"
  14. }
  15. }
英文:
  1. struct AllImage: Codable {
  2. let mediaReferenceID: String?
  3. let url: String?
  4. let title: String?
  5. let description: String?
  6. let icon: String?
  7. let tags: [TagInfo]?
  8. let footerSection: FooterSection?
  9. enum CodingKeys: String, CodingKey {
  10. case mediaReferenceID = "media_reference_id"
  11. case url, title, description, icon
  12. case ctaInfo = "cta_info"
  13. case footerSection = "footer_section"
  14. }
  15. }

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

Any solution to this problem?

答案1

得分: 1

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

  1. struct AllImage: Codable {
  2. let mediaReferenceID: String?
  3. let url: String?
  4. let title: String?
  5. let description: String?
  6. let icon: String?
  7. let tags: [TagInfo]?
  8. let footerSection: FooterSection?
  9. enum CodingKeys: String, CodingKey {
  10. case mediaReferenceID = "media_reference_id"
  11. case url, title, description, icon
  12. // Commented out this
  13. // case ctaInfo = "cta_info"
  14. // Add tags
  15. case tags = "tags"
  16. case footerSection = "footer_section"
  17. }
  18. }
  19. struct TagInfo : Codable {
  20. }
  21. struct FooterSection : Codable {
  22. }
英文:

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

  1. struct AllImage: Codable {
  2. let mediaReferenceID: String?
  3. let url: String?
  4. let title: String?
  5. let description: String?
  6. let icon: String?
  7. let tags: [TagInfo]?
  8. let footerSection: FooterSection?
  9. enum CodingKeys: String, CodingKey {
  10. case mediaReferenceID = "media_reference_id"
  11. case url, title, description, icon
  12. // Commented out this
  13. // case ctaInfo = "cta_info"
  14. // Add tags
  15. case tags = "tags"
  16. case footerSection = "footer_section"
  17. }
  18. }
  19. struct TagInfo : Codable {
  20. }
  21. struct FooterSection : Codable {
  22. }

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:

确定