Skip to content

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

License

Notifications You must be signed in to change notification settings

tablexi/AlamofireGenome

Repository files navigation

AlamofireGenome (NOT MAINTAINED)

THIS LIBRARY IS NO LONGER MAINTAINED. IF YOU'RE LOOKING FOR A SIMPLE WAY TO SERIALIZE/DESERIALIZE JSON WITH ALAMOFIRE, CONSIDER USING CodeableAlamofire.

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published