Moya-Decodable 0.2.0

Moya-Decodable 0.2.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Oct 2017
SwiftSwift Version 3.2
SPMSupports SPM

Maintained by xiaoyaogaojian.


Downloads

Total492
Week0
Month0

Installs

Apps38
Test Targets2
powered by Segment

GitHub

Stars3
Watchers1
Forks0
Issues0
Contributors0
Pull Requests0

Code

Files3
LOCLines of Code 34


  • By
  • Gao Jian




Decodable mappings for Moya network requests.

Installation

  • Using CocoaPods:
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, [email protected]

License

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