tristanhimmelman / ObjectMapper

Simple JSON Object mapping written in Swift
MIT License
9.16k stars 1.03k forks source link

How to use Generic,like HandyJSON? #1149

Open zhaoxianxin opened 6 months ago

zhaoxianxin commented 6 months ago

struct DSBaseResponse: Mappable { init?(map: Map) { }

var code = 0
private var msg: String?
var data: T?
/// 适配没有套到data层中
var total = 0
var rows: T?

mutating func mapping(map: Map) {
    msg <- map["message"]
    total <- map["total"]
    code <- map["code"]
    rows <- map["rows"]
    data <- map["data"]
}

}

T maybe is Any,[T], [String, Any],Int ... I cannot let T must confirm protocol Mappable, such as :

struct DSBaseResponse: Mappable { }

how can I design this struct? like HandyJSON, HandyJSON can use like this.

// test let res = .... network response string let result = DSBaseResponse(JSONString: res.string)