Skip to content

Instantly share code, notes, and snippets.

@matt-lebl
Last active August 29, 2015 14:27
Show Gist options
  • Save matt-lebl/2c21b046f5dcf5b469b4 to your computer and use it in GitHub Desktop.
Save matt-lebl/2c21b046f5dcf5b469b4 to your computer and use it in GitHub Desktop.
Using Alamofire and SwiftyJSON to get data from an online source.
//
// ViewController.swift
//
// Created by Matt Lebl on 2015-08-09.
// Copyright (c) 2015 Matt Lebl. All rights reserved.
//
import UIKit
import Alamofire
import SwiftyJSON
class ViewController: UIViewController {
let userToCheck = "chrislo27"
override func viewDidLoad() {
super.viewDidLoad()
// Use Alamofire to request repo data for a user from GitHub API
Alamofire.request(.GET, "https://api.github.com/users/\(userToCheck)/repos").responseJSON { (request, response, rawJSON, error) -> Void in
// Convert returned ambiguous type to SwiftyJSON
let json = JSON(rawJSON!)
// Iterate through all of the user's repos and print them along with the index
for (index: String, subJSON: JSON) in json {
println("Index: \(index)")
println(subJSON["name"].stringValue)
println()
}
}
}
//
// User: blerchy
// Console Output:
//
// Index: 0
// CzechStation
//
// Index: 1
// elginfitness
//
// Index: 2
// Hermes
//
// Index: 3
// LucidMaths
//
// Index: 4
// PlaylistBrowserSwift
//
//
// User: chrislo27
// Console output:
//
// Index: 0
// BeefaloWars
//
// Index: 1
// funwithgithubpages
//
// Index: 2
// LucidMaths
//
// Index: 3
// ProjectMP
//
// Index: 4
// Stray-core
//
// Index: 5
// Stray-desktop
//
// Index: 6
// Stray-goawaythisisnotusedanymore
//
// Index: 7
// VersionPlace
//
// Index: 8
// YourTicketPlease
//
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment