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.
Add the following to your Podfile
:
pod 'AlamofireGenome'
Add the following to your Cartfile
:
github "tablexi/AlamofireGenome"
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"]
}
}
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): // ...
}
}
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): // ...
}
}
git clone <repo_url>
- cd AlamofireGenome
- ./bin/setup