Ekra
Ekra

Reputation: 3301

iOS Generic type for codable property in Swift

I need to get a generic variable for a struct for parsing a JSON

but there is an error that I am getting Type 'BaseJsonModel' does not conform to protocol 'Codable

Below is my struct

  struct BaseJsonStruct<T>: Codable {
    let info: String
    let data: T
 }

Error:- Type 'BaseJsonModel' does not conform to protocol 'Codable'

Upvotes: 30

Views: 12489

Answers (1)

vadian
vadian

Reputation: 285220

T must also conform to Codable

struct BaseJsonStruct<T : Codable> : Codable {
    let info: String
    let data: T
}

Upvotes: 60

Related Questions