Skip to content

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

License

Notifications You must be signed in to change notification settings

perforator85/AlamofireObjectMapper

 
 

Repository files navigation

AlamofireObjectMapper

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 this extension as the follows:

let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/d8bb95982be8a11a2308e779bb9a9707ebe42ede/sample_json"
Alamofire.request(.GET, URL, parameters: nil).responseObject { (response: WeatherResponse?, error: NSError?) in
    println(response?.location)
    if let threeDayForecast = response?.threeDayForecast {
        for forecast in threeDayForecast {
            println(forecast.day)
            println(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]?
    
    init() {}
    
    required init?(_ map: Map) {
        mapping(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?
    
    init() {}
    
    required init?(_ map: Map) {
        mapping(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 are the three functions which you can use to have your responses mapped to objects. Just replace T with your custom response object and the extension handles the rest:

func responseObject<T: Mappable>(completionHandler: (T?, NSError?) -> Void) -> Self
func responseObject<T: Mappable>(completionHandler: (NSURLRequest, NSHTTPURLResponse?, T?, AnyObject?, NSError?) -> Void) -> Self
func responseObject<T: Mappable>(queue: dispatch_queue_t?, completionHandler: (NSURLRequest, NSHTTPURLResponse?, T?, AnyObject?, NSError?) -> Void) -> Self

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

pod 'AlamofireObjectMapper', '~> 0.1'

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

github "tristanhimmelman/AlamofireObjectMapper" ~> 0.1

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 82.2%
  • Ruby 9.9%
  • C++ 7.9%