Swiftpack.co - Package - xiaoyaogaojian/Moya-Decodable

This project is deprecated since Moya had already added a Decodable object mapping methods to Moya.Response from 10.0.

Moya-Decodable

Build Status CocoaPods License Platform

Decodable mappings for Moya network requests.

Dependencies

  • Swift 3.x
  • Moya 9.x

Installation

pod 'Moya-Decodable'
# or
pod 'Moya-Decodable/RxSwift'

Usage

Decodable

The first step is having a class / struct which conforms to the Decodable protocol:

struct User: Decodable {

  let id: Int
  let name: String
  
  let birthday: Date?
  
}

Moya.Response mapping

If you have a request setup with Moya already, you can use the map(to:) on the response:

provider
  .request(.allUsers) { result in
    if case let .Success(response) = result {
      do {
        let users: [User] = try response.map(to: [User].self)
        print("mapped to users: \(users)")
      } catch {
        print("Error mapping users: \(error)")
      }
    }
  }

RxSwift

If you are using the Moya RxSwift extensions, there is an extension on Observable which will simplify the mapping:

provider
  .request(.allUsers)
  .map(to: [User].self)
  .observeOn(MainScheduler.instance)
  .subscribeNext { users in

    self.users = users
    self.tableView.reloadData()

  }
  .disposed(by: disposeBag)

Contributing

Issues and pull requests are welcome

Author

Gao Jian, xiaoyaogaojian@outlook.com

License

Moya-Decodable is available under the MIT license. See the LICENSE file for more info.

Github

link
Stars: 11
Help us keep the lights on

Used By

Total: 0

Releases

0.2.0 - Oct 11, 2017

0.1.0 - Sep 25, 2017