GithubHelp home page GithubHelp logo

ataibarkai / moya-swiftyjsonmapper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from avdlee/moya-swiftyjsonmapper

0.0 1.0 0.0 527 KB

Map objects through SwiftyJSON in combination with Moya

License: MIT License

Swift 64.06% Ruby 0.18% Objective-C 32.21% DTrace 0.03% C 2.20% Shell 1.32%

moya-swiftyjsonmapper's Introduction

Moya-SwiftyJSONMapper

Version License Platform

Installation

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"

Usage

Example project

To run the example project, clone the repo, and run pod install from the Example directory first. It includes sample code and unit tests.

Model definitions

Create a Class or Struct which implements the Mappable protocol.

import Foundation
import Moya_SwiftyJSONMapper
import SwiftyJSON

final class GetResponse : ALSwiftyJSONAble {
    
    let url:NSURL?
    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]
    }
    
}

1. Without RxSwift or ReactiveCocoa

stubbedProvider.request(ExampleAPI.GetObject) { (result) -> () in
    switch result {
    case let .Success(response):
        do {
            let getResponseObject = try response.mapObject(GetResponse)
            print(getResponseObject)
        } catch {
            print(error)
        }
    case let .Failure(error):
        print(error)
    }
}

2. With ReactiveCocoa

RCStubbedProvider.request(ExampleAPI.GetObject).mapObject(GetResponse).on(failed: { (error) -> () in
    print(error)
}) { (response) -> () in
    print(response)
}.start()

3. With RxSwift

let disposeBag = DisposeBag()

RXStubbedProvider.request(ExampleAPI.GetObject).mapObject(GetResponse).subscribe(onNext: { (response) -> Void in
    print(response)
}, onError: { (error) -> Void in
    print(error)
}).addDisposableTo(disposeBag)

ObjectMapper

If you're using ObjectMapper, see Moya-ObjectMapper which inspired me to create this one.

Author

Antoine van der Lee

Mail: [email protected]
Home: www.avanderlee.com
Twitter: @twannl

License

Moya-SwiftyJSONMapper is available under the MIT license. See the LICENSE file for more info.

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.