Skip to content

Instantly share code, notes, and snippets.

@aaronksaunders
Last active January 19, 2016 00:41
Show Gist options
  • Select an option

  • Save aaronksaunders/fa9aa8851c9cc5ce4d03 to your computer and use it in GitHub Desktop.

Select an option

Save aaronksaunders/fa9aa8851c9cc5ce4d03 to your computer and use it in GitHub Desktop.
//
// NSURLSession and JSON Objects
//
// References:
// - https://littlebitesofcocoa.com/136-xcplayground-basics
//
import UIKit
import Foundation
import XCPlayground
// Let asynchronous code run
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
var session = NSURLSession.sharedSession()
var url:NSURL = NSURL(string: "https://randomuser.me/api/?results=3")!
var task = session.dataTaskWithURL( url, completionHandler: {
(data, response, error) -> Void in
do {
if error != nil {
print("Error is \(error!.localizedDescription)")
} else {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
// we get back results which is an array of user dictionaries
let users = json["results"] as? [[String: AnyObject]]
// loop through the user's getting information from the
// dictionary, remember some properties are respresnted and other object
// not strings and must be handled..
for user in users! {
// the user property is an object/dictionary
let a = user["user"] as! [String: AnyObject]
let aName = a["name"] as! [String: AnyObject]
// the gender property is just a string
let gender = a["gender"] as! String
print("Users: \(aName["title"]!) \(aName["first"]!) \(aName["last"]!) : \(gender)")
}
let userObject = json["results"] as? [[String: AnyObject]]
//let user = userObject["user"] as? [[String: AnyObject]]
//print("Result USER \(userObject!["user"])")
}
XCPlaygroundPage.currentPage.finishExecution()
} catch {
XCPlaygroundPage.currentPage.finishExecution()
}
})
task.resume()

This is a sample of the JSON output from using randomuser.me API

https://randomuser.me/documentation

{
  results: [{
    user: {
      gender: "female",
      name: {
        title: "ms",
        first: "manuela",
        last: "velasco"
      },
      location: {
        street: "1969 calle de alberto aguilera",
        city: "la coruña",
        state: "asturias",
        zip: "56298"
      },
      email: "[email protected]",
      username: "heavybutterfly920",
      password: "enterprise",
      salt: ">egEn6YsO",
      md5: "2dd1894ea9d19bf5479992da95713a3a",
      sha1: "ba230bc400723f470b68e9609ab7d0e6cf123b59",
      sha256: "f4f52bf8c5ad7fc759d1d4156b25a4c7b3d1e2eec6c92d80e508aa0b7946d4ba",
      registered: "1303647245",
      dob: "415458547",
      phone: "994-131-106",
      cell: "626-695-164",
      DNI: "52434048-I",
      picture: {
        large: "http://api.randomuser.me/portraits/women/39.jpg",
        medium: "http://api.randomuser.me/portraits/med/women/39.jpg",
        thumbnail: "http://api.randomuser.me/portraits/thumb/women/39.jpg",
      },
      version: "0.6"
      nationality: "ES"
    },
    seed: "graywolf"
  }]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment