简体   繁体   English

是否可以在 Decodable 模型中声明泛型

[英]Is it possible to declare generics inside Decodable model

Here is my model class这是我的模型课

struct ErrorData: Decodable {
    let code : Int
    let message : String
    let data : [ErrorDataFields]
}

i want to have ErrorDataFields to be array and object like我想让ErrorDataFields成为数组和对象

struct ErrorData: Decodable {
        let code : Int
        let message : String
        let data : [ErrorDataFields]
    }

AND

struct ErrorData: Decodable {
        let code : Int
        let message : String
        let data : ErrorDataFields
    }

We can use a generic type with Decodable.我们可以使用带有 Decodable 的泛型类型。 Only thing is the generic type should also conform to Decodable.唯一的事情是泛型类型也应该符合 Decodable。

struct ErrorData <T : Decodable> : Decodable {
    let code : Int
    let message : String
    let data : T
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM