swifttuplescodableswift-protocolsswift-optionals

Swift struct with tuples does not conform to Codable


I'm trying to make a struct with optionals Codable/Decodable but I receive the 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 make the tuple [(testoRisposta: String, valoreRisposta: Bool)]? conform?


Solution

  • You need

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