2

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?

3
  • 2
    You should make (testoRisposta: String, valoreRisposta: Bool) into another struct. Commented Apr 18, 2020 at 10:07
  • Tuples cannot be made to conform to Codable (see forums.swift.org/t/codable-tuples/14174). What does the JSON(?) for this look like? Commented Apr 18, 2020 at 10:10
  • Possible duplicate of How to make Tuple conform a Protocol in Swift? Commented Apr 25, 2024 at 23:41

1 Answer 1

4

You need

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

struct InnerItem: Codable { 
   var testoRisposta: String
   var valoreRisposta: Bool
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.