TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Oct 2017 |
SwiftSwift Version | 3.2 |
SPMSupports SPM | ✓ |
Maintained by xiaoyaogaojian.
Decodable mappings for Moya network requests.
pod 'Moya-Decodable'
# or
pod 'Moya-Decodable/RxSwift'
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?
}
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)")
}
}
}
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)
Issues and pull requests are welcome
Gao Jian, [email protected]
Moya-Decodable is available under the MIT license. See the LICENSE file for more info.