Reputation: 1037
I am new to Swift and am having a spot of bother with a decodable class. I am receiving an error on this class : Type 'VoucherCode' does not conform to protocol 'Decodable'
I have exactly the same syntax in another project without error which came from a tutorial.
Without the published line, the class works and is decoded from relevant json.
What am I missing please?
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()
}
}
}
A similar class (which works) from a CodeWithChris lesson. There is no error and it works.
Upvotes: 0
Views: 410
Reputation: 36314
add this to your class:
private enum CodingKeys: String, CodingKey {
case id, title, voucherCode, details, logo, url, termsAndConditions, highlight, whoFor
}
this will exclude logoData from the decodable and make VoucherCode Decodable.
Upvotes: 3