Skip to content

SebastianCrow/Moya-ObjectMapper

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

68 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Moya-ObjectMapper

CocoaPods

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

Installation

CocoaPods

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'

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

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 68.5%
  • Objective-C 28.8%
  • C 2.0%
  • Other 0.7%