GithubHelp home page GithubHelp logo

droiddreamer / codablealamofire Goto Github PK

View Code? Open in Web Editor NEW

This project forked from otbivnoe/codablealamofire

1.0 0.0 0.0 101 KB

An extension for Alamofire that converts JSON data into Decodable objects.

License: MIT License

Ruby 8.27% Swift 88.30% Objective-C 3.43%

codablealamofire's Introduction

CodableAlamofire

Swift 4.0.x 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, responseJSON - responseDecodableObject:

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

Because of Alamofire supports Swift 3 at the moment and we can't point out the branch of dependency in .podspec file. Whe have to overrride the current pod:

# TODO: Remove this after all pods are converted to swift 4
def swift4_overrides
    pod 'Alamofire', git: 'https://github.com/Alamofire/Alamofire.git', branch: 'swift4'
end

target 'Test' do
  use_frameworks!

  swift4_overrides
  pod 'CodableAlamofire', :git => 'https://github.com/Otbivnoe/CodableAlamofire.git'
  
end

Carthage

NOTE: Don't forget to set the correct Command Line Tools: Xcode > Preferences > Locations > Command Line Tools > Xcode 9.0.

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.

codablealamofire's People

Contributors

otbivnoe avatar

Stargazers

Adam Walker avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.