To run the example project, clone the repo, and run pod install
from the Example directory first. It includes sample code and unit tests.
Create a Class
or Struct
which implements the Mappable
protocol.
import Foundation
import Moya_SwiftyJSONMapper
import SwiftyJSON
final class GetResponse : ALSwiftyJSONAble {
let url:URL?
let origin:String
let args:[String: String]?
required init?(jsonData:JSON){
self.url = jsonData["url"].URL
self.origin = jsonData["origin"].stringValue
self.args = jsonData["args"].object as? [String : String]
}
}
stubbedProvider.request(ExampleAPI.GetObject) { (result) -> () in
switch result {
case let .success(response):
do {
let getResponseObject = try response.map(to: GetResponse.self)
print(getResponseObject)
} catch {
print(error)
}
case let .failure(error):
print(error)
}
}
stubbedProvider.reactive.request(token: ExampleAPI.GetObject).map(to: GetResponse.self).on(failed: { (error) -> () in
print(error)
}) { (response) -> () in
print(response)
}.start()
let disposeBag = DisposeBag()
stubbedProvider.rx.request(ExampleAPI.GetObject).map(to: GetResponse.self).subscribe(onNext: { (response) -> Void in
print(response)
}, onError: { (error) -> Void in
print(error)
}).addDisposableTo(disposeBag)
Moya-SwiftyJSONMapper is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "Moya-SwiftyJSONMapper"
The subspec if you want to use the bindings over RxSwift.
pod "Moya-SwiftyJSONMapper/RxSwift"
And the subspec if you want to use the bindings over ReactiveCocoa.
pod "Moya-SwiftyJSONMapper/ReactiveCocoa"
If you're using JASON for parsing JSON data, checkout Moya-JASONMapper.
If you're really into reactive programming, checkout ALDataRequestView and handle those edge cases with ease!
Antoine van der Lee
Mail: [email protected]
Home: www.avanderlee.com
Twitter: @twannl
Moya-SwiftyJSONMapper is available under the MIT license. See the LICENSE file for more info.