繁体   English   中英

Swift Decodable Class 不符合

[英]Swift Decodable Class does not conform

我是 Swift 的新手,并且对可解码类有一点麻烦。 我收到关于此类的错误:类型“VoucherCode”不符合协议“Decodable”

我在另一个项目中具有完全相同的语法,没有来自教程的错误。

如果没有发布的行,该类可以工作并从相关的 json 中解码。

请问我错过了什么?

import Foundation

class VoucherCode: Decodable, Identifiable, ObservableObject {

@Published var logoData: Data?

var id:UUID?
var title: String?
var voucherCode: String?
var details: String?
var logo: String?
var url: String?
var termsAndConditions: String?
var highlight: String?
var whoFor:[String]?



func getLogoData() {
    guard logo != nil else {
        return
    }
    
    if let url = URL(string: logo!) {
        let session = URLSession.shared
        let dataTask = session.dataTask(with: url) { (data, response, error) in
            if error == nil {
                DispatchQueue.main.async {
                self.logoData = data!
                }
            }
        }
        
        dataTask.resume()
    }
}
}

CodeWithChris 课程中的类似课程(有效)。 没有错误,它可以工作。

工人阶级

将此添加到您的课程中:

private enum CodingKeys: String, CodingKey {
    case id, title, voucherCode, details, logo, url, termsAndConditions, highlight, whoFor
}

这将从可解码中排除 logoData 并使 VoucherCode 可解码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM