英文:
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 {
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论