Latest 0.2.0
Homepage https://github.com/xiaoyaogaojian/Moya-Decodable
License MIT
Platforms ios 8.0, osx 10.11, tvos 9.0, watchos 2.0
Authors

Build Status
CocoaPods
License
Platform

Decodable mappings for Moya network requests.

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, [email protected]

License

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

Latest podspec

{
    "name": "Moya-Decodable",
    "version": "0.2.0",
    "summary": "Decodable mappings for Moya network requests",
    "description": "Extensions to simplify mapping Moya responses to Decodable",
    "homepage": "https://github.com/xiaoyaogaojian/Moya-Decodable",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Gao Jian": "[email protected]"
    },
    "source": {
        "git": "https://github.com/xiaoyaogaojian/Moya-Decodable.git",
        "tag": "0.2.0"
    },
    "platforms": {
        "ios": "8.0",
        "osx": "10.11",
        "tvos": "9.0",
        "watchos": "2.0"
    },
    "default_subspecs": "Core",
    "pushed_with_swift_version": "3.2",
    "subspecs": [
        {
            "name": "Core",
            "source_files": "Sources/Moya-Decodable/**/*.swift",
            "frameworks": "Foundation",
            "dependencies": {
                "Moya": [
                    "~> 9.0"
                ]
            }
        },
        {
            "name": "RxSwift",
            "source_files": "Sources/RxMoya-Decodable/**/*.swift",
            "dependencies": {
                "Moya-Decodable/Core": [],
                "RxSwift": [
                    "~> 3.0"
                ]
            }
        }
    ]
}

Pin It on Pinterest

Share This