Last active
April 18, 2020 12:56
-
-
Save meyusufdemirci/96041090aa020d3db527e571ea777d3f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ResponseModel<T: Codable>: Codable { | |
// MARK: - Properties | |
var isSuccess: Bool | |
var message: String | |
var error: ErrorModel { | |
return ErrorModel(message) | |
} | |
var rawData: Data? | |
var data: T? | |
var json: String? { | |
guard let rawData = rawData else { return nil } | |
return String(data: rawData, encoding: String.Encoding.utf8) | |
} | |
var request: RequestModel? | |
public init(from decoder: Decoder) throws { | |
let keyedContainer = try decoder.container(keyedBy: CodingKeys.self) | |
isSuccess = (try? keyedContainer.decode(Bool.self, forKey: CodingKeys.isSuccess)) ?? false | |
message = (try? keyedContainer.decode(String.self, forKey: CodingKeys.message)) ?? "" | |
data = try? keyedContainer.decode(T.self, forKey: CodingKeys.data) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment