Skip to content

Instantly share code, notes, and snippets.

@i0sa
Created March 25, 2020 12:02
Show Gist options
  • Save i0sa/cc85c1c86545b94879fc09ffc92e0c87 to your computer and use it in GitHub Desktop.
Save i0sa/cc85c1c86545b94879fc09ffc92e0c87 to your computer and use it in GitHub Desktop.
struct BaseResponse<T: Codable>: Codable {
var status: Bool?
var message: String?
var data: T
required init(from decoder:Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
//MARK: Decode all types of messages (message_ar, message, error)
if let message = try? values.decode(String.self, forKey: .message) {
self.message = message
} else if let message = try? values.decode(String.self, forKey: .message_ar) {
self.message = message
} else if let message = try? values.decode(String.self, forKey: .error) {
self.message = message
}
//MARK: Decode/Init other stuff
self.status = try? values.decode(Bool.self, forKey: .status)
self.data = try? values.decode(T.self, forKey: .data)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment