项目介绍:
AlamofireCodable: An extension to Alamofire which automatically converts JSON response data into swift objects using Codable.
This project is heavily inspired by the popular AlamofireObjectMapper.
Installation
AlamofireCodable can be added to your project using CocoaPods by adding the following line to your Podfile:
pod 'AlamofireCodable'
Example
To run the example project, clone the repo, and run pod install
from the Example directory first.
Requirements
Xcode 9+ , Swift 4+
Usage
Given a URL which returns weather data in the following form:
{
"data":{
"location":"Toronto, Canada",
"three_day_forecast":[
{
"conditions":"Partly cloudy",
"day":"Monday",
"temperature":20
},
{
"conditions":"Showers",
"day":"Tuesday",
"temperature":22
},
{
"conditions":"Sunny",
"day":"Wednesday",
"temperature":28
}
]
}
}
You can use the extension as the follows:
import AlamofireCodable
let form = WeatherForm()
Alamofire.request(
form.url,
method: HTTPMethod.get,
parameters: form.parameters(),
encoding: form.encoding(),
headers: form.headers()
)
.responseObject(keyPath: "data",completionHandler: { (response: DataResponse<Weather>) in
switch response.result {
case .success(let object):
debugPrint("