morgz / Moya-ObjectMapper

ObjectMapper bindings for Moya and RxSwift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Moya-ObjectMapper

CocoaPods

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

Installation

CocoaPods

pod 'Moya-ObjectMapper', '~> 1.1.6'

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

pod 'Moya-ObjectMapper/RxSwift', '~> 1.1.6'

And the subspec if you want to use the bindings over ReactiveCocoa.

pod 'Moya-ObjectMapper/ReactiveCocoa', '~> 1.1.6'

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 <- map["id"]
    language <- map["language"]
    url <- map["url"]
  }

}

1. Without RxSwift

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.respos = 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)
  .subscribe { event -> Void in
    switch event {
    case .Next(let repos):
      self.repos = repos
    case .Error(let error):
      print(error)
    default:
      break
    }
  }

Contributing

Issues and pull requests are welcome!

Author

Ivan Bruel @ivanbruel

License

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

About

ObjectMapper bindings for Moya and RxSwift

License:MIT License


Languages

Language:Swift 66.3%Language:Ruby 33.7%