0
votes

I'm trying to let a struct with optionals being codable/decodable but I receive an error message:

Type 'item' does not conform to protocol 'Encodable'

here is the code:

struct Item: Codable {
    let domanda: String
    let rispostaSemplice: Int?
    var rispostaComplessa: [(testoRisposta: String, valoreRisposta: Bool)]?
}

How can I let [(testoRisposta: String, valoreRisposta: Bool)]? conform?

Thanks

1
You should make (testoRisposta: String, valoreRisposta: Bool) into another struct.Sweeper
Tuples cannot be made to conform to Codable (see forums.swift.org/t/codable-tuples/14174). What does the JSON(?) for this look like?Gereon

1 Answers

4
votes

You need

struct Item: Codable {
  let domanda: String
  let rispostaSemplice: Int?
  var rispostaComplessa: [InnerItem]?
}

struct InnerItem: Codable { 
   var testoRisposta: String
   var valoreRisposta: Bool
}