iOS Generic type for codable property in Swift

独自空忆成欢 提交于 2020-04-08 01:48:35

问题


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'


回答1:


T must also conform to Codable

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


来源:https://stackoverflow.com/questions/49529208/ios-generic-type-for-codable-property-in-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!