ObjectMapper bindings for Moya for easier JSON serialization. Includes RxSwift bindings as well.
pod 'Moya-ObjectMapper', '~> 1.3'
The subspec if you want to use the bindings over RxSwift.
pod 'Moya-ObjectMapper/RxSwift', '~> 1.3'
And the subspec if you want to use the bindings over ReactiveCocoa.
pod 'Moya-ObjectMapper/ReactiveCocoa', '~> 1.3'
Create a Class
or Struct
which implements the Mappable
protocol.
import Foundation
import ObjectMapper
// MARK: Initializer and Properties
struct Repository: Mappable {
var identifier: Int!
var language: String?
var url: String!
// MARK: JSON
init?(_ map: Map) { }
mutating func mapping(map: Map) {
identifier <- map["id"]
language <- map["language"]
url <- map["url"]
}
}
GitHubProvider.request(.UserRepositories(username), completion: { result in
var success = true
var message = "Unable to fetch from GitHub"
switch result {
case let .Success(response):
do {
if let repos = try response.mapArray(Repository) {
self.repos = repos
} else {
success = false
}
} catch {
success = false
}
self.tableView.reloadData()
case let .Failure(error):
guard let error = error as? CustomStringConvertible else {
break
}
message = error.description
success = false
}
})
GitHubProvider.request(.UserRepositories(username))
.mapArray(Repository)
.subscribe { event -> Void in
switch event {
case .Next(let repos):
self.repos = repos
case .Error(let error):
print(error)
default:
break
}
}.addDisposableTo(disposeBag)
Issues and pull requests are welcome!
Ivan Bruel @ivanbruel
Moya-ObjectMapper is released under an MIT license. See LICENSE for more information.