GithubHelp home page GithubHelp logo

bluelich / alamofireobjectmapper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tristanhimmelman/alamofireobjectmapper

0.0 2.0 0.0 134 KB

An Alamofire extension which converts JSON response data into swift objects using ObjectMapper

License: MIT License

Ruby 4.19% Objective-C 8.70% Swift 87.11%

alamofireobjectmapper's Introduction

AlamofireObjectMapper

Build Status CocoaPods Carthage compatible

An extension to Alamofire which automatically converts JSON response data into swift objects using ObjectMapper.

#Usage

Given a URL which returns weather data in the following form:

{
    "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:

let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/d8bb95982be8a11a2308e779bb9a9707ebe42ede/sample_json"
Alamofire.request(.GET, URL).responseObject { (response: Response<WeatherResponse, NSError>) in

    let weatherResponse = response.result.value
    print(weatherResponse?.location)
    
    if let threeDayForecast = weatherResponse?.threeDayForecast {
        for forecast in threeDayForecast {
            print(forecast.day)
            print(forecast.temperature)           
        }
    }
}

The WeatherResponse object in the completion handler is a custom object which you define. The only requirement is that the object must conform to ObjectMapper's Mappable protocol. In the above example, the WeatherResponse object looks like the following:

class WeatherResponse: Mappable {
    var location: String?
    var threeDayForecast: [Forecast]?
    
	required init?(_ map: Map){

	}
    
    func mapping(map: Map) {
        location <- map["location"]
        threeDayForecast <- map["three_day_forecast"]
    }
}

class Forecast: Mappable {
    var day: String?
    var temperature: Int?
    var conditions: String?
    
	required init?(_ map: Map){

	}
    
    func mapping(map: Map) {
        day <- map["day"]
        temperature <- map["temperature"]
        conditions <- map["conditions"]
    }
}

The extension uses Generics to allow you to create your own custom response objects. Below is the responseObject function definition. Just replace T in the completionHandler with your custom response object and the extension handles the rest:

public func responseObject<T: Mappable>(queue queue: dispatch_queue_t? = nil, keyPath: String? = nil, mapToObject object: T? = nil, completionHandler: Response<T, NSError> -> Void) -> Self

The responseObject function has 3 optional parameters and a required completionHandler:

  • queue: The queue on which the completion handler is dispatched.
  • keyPath: The key path of the JSON where object mapping should be performed
  • mapToObject: An object to perform the mapping on to
  • completionHandler: A closure to be executed once the request has finished and the data has been mapped by ObjectMapper.

###KeyPath

The keyPath variable is used to drill down into a JSON response and only map the data found at that keyPath. It supports nested values such as data.weather to drill down several levels in a JSON response.

let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/2ee8f34d21e8febfdefb2b3a403f18a43818d70a/sample_keypath_json"
let expectation = expectationWithDescription("\(URL)")

Alamofire.request(.GET, URL).responseObject(keyPath: "data") { (response: Response<WeatherResponse, NSError>) in
    expectation.fulfill()
    
    let weatherResponse = response.result.value
    print(weatherResponse?.location)
    
    if let threeDayForecast = weatherResponse?.threeDayForecast {
        for forecast in threeDayForecast {
            print(forecast.day)
            print(forecast.temperature)           
        }
    }
}

#Array Responses If you have an endpoint that returns data in Array form you can map it with the following function:

public func responseArray<T: Mappable>(queue queue: dispatch_queue_t? = nil, keyPath: String? = nil, completionHandler: Response<[T], NSError> -> Void) -> Self

For example, if your endpoint returns the following:

[
    { 
        "conditions": "Partly cloudy",
        "day" : "Monday",
        "temperature": 20 
    },
    { 
        "conditions": "Showers",
        "day" : "Tuesday",
        "temperature": 22 
    },
    { 
        "conditions": "Sunny",
        "day" : "Wednesday",
        "temperature": 28 
    }
]

You can request and map it as follows:

let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/f583be1121dbc5e9b0381b3017718a70c31054f7/sample_array_json"
Alamofire.request(.GET, URL).responseArray { (response: Response<[Forecast], NSError>) in

    let forecastArray = response.result.value
    
    if let forecastArray = forecastArray {
        for forecast in forecastArray {
            print(forecast.day)
            print(forecast.temperature)           
        }
    }
}

#Installation AlamofireObjectMapper can be added to your project using CocoaPods by adding the following line to your Podfile:

pod 'AlamofireObjectMapper', '~> 3.0'

If your using Carthage you can add a dependency on AlamofireObjectMapper by adding it to your Cartfile:

github "tristanhimmelman/AlamofireObjectMapper" ~> 3.0

alamofireobjectmapper's People

Contributors

alexseverinov avatar ikesyo avatar louisdh avatar mabril avatar nevstad avatar patricks avatar pizthewiz avatar readmecritic avatar schayes04 avatar tristanhimmelman avatar

Watchers

 avatar  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.