Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Generic type for codable property in Swift

Tags:

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'

like image 933
Ekra Avatar asked Mar 28 '18 08:03

Ekra


People also ask

What types are Codable Swift?

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.

What is Codable in Swift IOS?

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.

What is Codable in IOS?

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.


1 Answers

T must also conform to Codable

struct BaseJsonStruct<T : Codable> : Codable {     let info: String     let data: T } 
like image 77
vadian Avatar answered Sep 19 '22 18:09

vadian