Skip to content

Instantly share code, notes, and snippets.

@alexnikol
Created August 17, 2020 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexnikol/e0e3b703276a7f62a638504b28deb26a to your computer and use it in GitHub Desktop.
Save alexnikol/e0e3b703276a7f62a638504b28deb26a to your computer and use it in GitHub Desktop.
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