Skip to content

spxrogers/Alamofire-Gloss

Repository files navigation

Alamofire-Gloss

CocoaPods Carthage compatible

Gloss bindings for Alamofire for easy-peasy JSON serialization.

Installation

CocoaPods

Add to your Podfile:

pod 'Alamofire-Gloss'

Carthage

github "spxrogers/Alamofire-Gloss"

Alamofire-Gloss lists Alamofire and Gloss as explicit Carthage dependencies, so it's only necessary to list Alamofire-Gloss in your Cartfile and it will pull down all three libraries. Copy & Link generated frameworks as normal.

Usage

Define your Model

Create a Class or Struct which implements the Decodable (or Glossy) protocol.

import Foundation
import Gloss

struct Person: JSONDecodable {

  let name: String
  let age: Int?

  init?(json: JSON) {
    guard let name: String = "name" <~~ json
      else { return nil }
    
    self.name = name
    self.age = "age" <~~ json
  }
}

API

responseObject()
responseArray()

1. Example – mapObject

Alamofire.request(personUrl).responseObject(Person.self) { (response) in
  switch response.result {
  case .success(let person):
    print("Found person: \(person)")
  case .failure(let error):
    print("Error'd: \(error)")
  }
}

2. Example – mapArray

Alamofire.request(peopleUrl).responseArray(Person.self) { (response) in
  switch response.result {
  case .success(let people):
    print("Found people: \(people)")
  case .failure(let error):
    print("Error'd: \(error)")
  }
}

Contributing

Issues and pull requests are welcome!

Author

Steven Rogers @spxrogers

Thanks ...

... to Harlan Kellaway for creating Gloss, my preferred JSON library :)

... to the Alamofire (+AFNetworking) team for paving the way of Networking in iOS.

License

Alamofire-Gloss is released under an MIT license. See LICENSE for more information.

Smile for a permissive license.