Skip to content

Instantly share code, notes, and snippets.

@meyusufdemirci
Last active April 18, 2020 12:56
Show Gist options
  • Save meyusufdemirci/96041090aa020d3db527e571ea777d3f to your computer and use it in GitHub Desktop.
Save meyusufdemirci/96041090aa020d3db527e571ea777d3f to your computer and use it in GitHub Desktop.
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