How to keep a flexible structure when using Codable in Swift

百般思念 提交于 2020-01-06 04:30:06

问题


I've got a API response structure, representing a user object, that looks like this:

{
    "statuscode": 200,
    "response_type": 3,
    "errormessage": null,
    "detailresponse": {
        "id": "2",
        "shopifyautosync": null,
        "platformfeepercentage": null,
        "invited": null,
        "requiresyoutubesocialmediaupdate": 1,
        // Other properties ...
}

I'm using JSONDecoder().decode to decode to following structures:

import Foundation

class Response: Decodable {
    var statuscode: Int?
    var response_type: Int?
    // Other properties
    var detailresponse: User?
}

import Foundation

class User: Codable {
    var id: String?
    var street: String?
    var supporturl: String?
    var verifiedaccount: Int?
    var showfeatureupdatemodal: Int?
    var admin: Int?
    var email: String?
    // Other properties
}

Here's how I then decode:

let response = try JSONDecoder().decode(Response.self, from: jsonData)

My main problem now is that the Response class' detailresponse property is hard-wired to a User struct. However, I need a little flexibility in my setup, as the detailresponse will, of course, carry other data structures when calling different endpoints (e.g. a cooperation object instead of a user object).

Is there an elegant way to keep the detailresponse inside the Response class flexible instead of hard-wiring it? Or a generally better approach to the problem instead?


回答1:


You need to make use of generics

class Response<T:Decodable>: Decodable {
    var statuscode: Int?
    var response_type: Int?
    // Other properties
    var detailresponse: T?
}

Then

let response = try JSONDecoder().decode(Response<User>.self, from: jsonData)



回答2:


This is a use case for generics:

class Response<T: Decodable>: Decodable {
   var statuscode: Int?
   var response_type: Int?
   // Other properties
   var detailresponse: T?
}

Note there is little reason to make the properties mutable. They should be let. Also, a struct would probably be a better choice here. And I think there should be less optionals since this is supposed to be a success response.



来源:https://stackoverflow.com/questions/59518330/how-to-keep-a-flexible-structure-when-using-codable-in-swift

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