Skip to content

Instantly share code, notes, and snippets.



Created Aug 17, 2020
What would you like to do?
Generic way to solve pagination duplications
class Pagination<Element: Decodable>: Decodable {
var list: [Element]
var currentPage: Int
var totalPages: Int
enum ResultCodingKeys: String, CodingKey {
case list, currentPage, totalPages
required init(from decoder: Decoder) throws {
let objectsContainer = try decoder.container(keyedBy: ResultCodingKeys.self)
currentPage = try objectsContainer.decode(Int.self, forKey: .currentPage)
totalPages = try objectsContainer.decode(Int.self, forKey: .totalPages)
list = try objectsContainer.decode([Element].self, forKey: .list)
//Usage part!
var paginationPet: Pagination<Pet>?
var paginationUser: Pagination<User>?
var paginationVet: Pagination<Vet>?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.