Moya-ObjectMapper

by bmoliveira

bmoliveira /Moya-ObjectMapper

ObjectMapper bindings for Moya and RxSwift

454 Stars 144 Forks Last release: 8 months ago (2.9) MIT License 183 Commits 32 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

Moya-ObjectMapper

CocoaPods Swift 4.2

ObjectMapper bindings for Moya for easier JSON serialization. Includes RxSwift bindings as well.

Installation

CocoaPods

pod 'Moya-ObjectMapper'

The subspec if you want to use the bindings over RxSwift.

pod 'Moya-ObjectMapper/RxSwift'

The subspec if you want to use the bindings over ReactiveSwift.

pod 'Moya-ObjectMapper/ReactiveSwift'

Usage

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

1. Without RxSwift and ReactiveSwift

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
}

})

2. With RxSwift

GitHubProvider.request(.userRepositories(username))
  .mapArray(Repository.self)
  .subscribe { event -> Void in
    switch event {
    case .next(let repos):
      self.repos = repos
    case .error(let error):
      print(error)
    default: break
    }
  }.addDisposableTo(disposeBag)

2. With ReactiveSwift

GitHubProvider.request(.userRepositories(username))
  .mapArray(Repository.self)
  .start { event in
    switch event {
    case .value(let repos):
      self.repos = repos
    case .failed(let error):
      print(error)
    default: break
    }
  }

Contributing

Issues and pull requests are welcome!

Author

Ivan Bruel @ivanbruel

Maintainers

Bruno Oliveira @bmoliveira

License

Moya-ObjectMapper is released under an MIT license. See LICENSE for more information.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.