博客> JSON Parsing With Codable
JSON Parsing With Codable
2019-12-12 07:20 评论:0 阅读:1305 KingCQ
ios swift jian

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.

image.png 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("
收藏
0
sina weixin mail 回到顶部