Created
March 25, 2020 12:02
-
-
Save i0sa/cc85c1c86545b94879fc09ffc92e0c87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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