Latest 1.1.1
Homepage https://github.com/Otbivnoe/CodableAlamofire
License MIT
Platforms ios 8.0, osx 10.10, watchos 2.0, tvos 9.0, requires ARC
Dependencies Alamofire
Authors

CodableAlamofire

Build Status
Swift 4.0.x
SPM compatible
Carthage Compatible
Version
platforms

Swift 4 introduces a new Codable protocol that lets you serialize and deserialize custom data types without writing any special code and without having to worry about losing your value types. 🎉

Awesome, isn’t it? And this library helps you write less code! It’s an extension for Alamofire that converts JSON data into Decodable object.

Useful Resources:

Usage

Let’s decode a simple json file:

{
    "result": {
        "libraries": [
            {
                "name": "Alamofire",
                "stars": 23857,
                "url": "https://github.com/Alamofire/Alamofire",
                "random_date_commit": 1489276800
            },
            {
                "name": "RxSwift",
                "stars": 9600,
                "url": "https://github.com/ReactiveX/RxSwift",
                "random_date_commit": 1494547200
            }   
        ]
    }
}

with the following Swift model:

private struct Repo: Decodable {
    let name: String
    let stars: Int
    let url: URL
    let randomDateCommit: Date

    private enum CodingKeys: String, CodingKey {
        case name
        case stars
        case url
        case randomDateCommit = "random_date_commit"
    }
}

There is a similar method to responseData, responseJSONresponseDecodableObject:

func responseDecodableObject<T: Decodable>(queue: DispatchQueue? = nil, keyPath: String? = nil, decoder: JSONDecoder = JSONDecoder(), completionHandler: @escaping (DataResponse<T>) -> Void)
  • queue – The queue on which the completion handler is dispatched.
  • keyPath – The keyPath where object decoding should be performed.
  • decoder – The decoder that performs the decoding of JSON into semantic Decodable type.
let url = URL(string: "https://raw.githubusercontent.com/otbivnoe/CodableAlamofire/master/keypathArray.json")!
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .secondsSince1970 // It is necessary for correct decoding. Timestamp -> Date.

Alamofire.request(url).responseDecodableObject(keyPath: "result.libraries", decoder: decoder) { (response: DataResponse<[Repo]>) in
    let repo = response.result.value
    print(repo)
}

Note:

  • For array: DataResponse<[Repo]>
  • For single object: DataResponse<Repo>

Requirements

  • Swift 4+
  • Xcode 9+

Installation 🔥

CocoaPods

CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. It has over eighteen thousand libraries and can help you scale your projects elegantly. You can install it with the following command:

$ sudo gem install cocoapods

To integrate CodableAlamofire, simply add the following line to your Podfile:

target 'Test' do
  use_frameworks!

  pod 'CodableAlamofire'

end

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate CodableAlamofire into your Xcode project using Carthage, specify it in your Cartfile:

github "Otbivnoe/CodableAlamofire"

Run carthage update to build the framework and drag the built CodableAlamofire.framework into your Xcode project.

Latest podspec

{
    "name": "CodableAlamofire",
    "version": "1.1.1",
    "summary": "An extension for Alamofire that converts JSON data into Decodable Objects.",
    "homepage": "https://github.com/Otbivnoe/CodableAlamofire",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Nikita Ermolenko": "[email protected]"
    },
    "social_media_url": "https://twitter.com/iOtbivnoe",
    "platforms": {
        "ios": "8.0",
        "osx": "10.10",
        "watchos": "2.0",
        "tvos": "9.0"
    },
    "source": {
        "git": "https://github.com/Otbivnoe/CodableAlamofire.git",
        "tag": "1.1.1"
    },
    "source_files": "Sources/**/*.{h,swift}",
    "requires_arc": true,
    "dependencies": {
        "Alamofire": [
            "~> 4.0"
        ]
    }
}

Pin It on Pinterest

Share This