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'
There are many types in Swift that are codable out of the box: Int , String , Date , Array and many other types from the Standard Library and the Foundation framework. If you want your type to be codable, the simplest way to do it is by conforming to Codable and making sure all its stored properties are also codable.
Codable is the combined protocol of Swift's Decodable and Encodable protocols. Together they provide standard methods of decoding data for custom types and encoding data to be saved or transferred.
Codable is a type alias for the Encodable and Decodable protocols. When you use Codable as a type or a generic constraint, it matches any type that conforms to both protocols.
T
must also conform to Codable
struct BaseJsonStruct<T : Codable> : Codable { let info: String let data: T }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With