Swiftpack.co - Package - tablexi/AlamofireGenome

AlamofireGenome

An Alamofire extension that automatically converts JSON into Swift objects using Genome.

Installation

Cocoapods

Add the following to your Podfile:

pod 'AlamofireGenome'

Carthage

Add the following to your Cartfile:

github "tablexi/AlamofireGenome"

Usage

Let's pretend we're interested in querying the Github API for repository information. To store retrieved data, we can define the following simple data structure:

struct GithubRepository {
  var name: String!
  var fullName: String!
  var description: String!
}

extension GithubRepository: BasicMappable {
  mutating func sequence(map: Map) throws {
    try name <~> map["name"]
    try fullName <~> map["full_name"]
    try description <~> map["description"]
  }
}

Fetching a single object

To fetch a single repository, we can use the following code:

let url = "https://api.github.com/repos/tablexi/AlamofireGenome"
Alamofire.request(.GET, url).responseObject { (response: Response<GithubRepository, NSError>) in
  switch response.result {
    case .Success(let repository): // ...
    case .Failure(let error): // ...
  }
}

Fetching an array of objects

To fetch a list of repositories, we can use the following code:

let url = "https://api.github.com/orgs/tablexi/repos"
Alamofire.request(.GET, url).responseArray { (response: Response<[GithubRepository], NSError>) in
  switch response.result {
    case .Success(let repositories): // ...
    case .Failure(let error): // ...
  }
}

Contributing

  1. git clone <repo_url>
  2. cd AlamofireGenome
  3. ./bin/setup

Github

link
Stars: 3
Help us keep the lights on

Used By

Total: 0

Releases

1.0.1 - Mar 14, 2016

Added

Added Swift Package Manager support.

Updated

Updated the setup script to use carthage bootstrap instead of update.

1.0.0 - Feb 26, 2016

An Alamofire extension that automatically converts JSON into Swift objects using Genome.