0

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

Louis Lac
  • 5,298
  • 1
  • 21
  • 36
Manuel Zompetta
  • 394
  • 4
  • 17
  • 2
    You should make `(testoRisposta: String, valoreRisposta: Bool)` into another struct. – Sweeper Apr 18 '20 at 10:07
  • Tuples cannot be made to conform to Codable (see https://forums.swift.org/t/codable-tuples/14174). What does the JSON(?) for this look like? – Gereon Apr 18 '20 at 10:10

2 Answers2

3

You need

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

struct InnerItem: Codable { 
   var testoRisposta: String
   var valoreRisposta: Bool
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0

You can paste your JSON here, and get the struct of your JSON.

https://quicktype.io

There are more features regarding making a JSON object, which will help you in future.