Skip to content

Instantly share code, notes, and snippets.

@iTSangarDEV
Last active May 4, 2016 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iTSangarDEV/94aeafe88c3afecf2ace0fcefb06215f to your computer and use it in GitHub Desktop.
Save iTSangarDEV/94aeafe88c3afecf2ace0fcefb06215f to your computer and use it in GitHub Desktop.
Playground para a questão http://pt.stackoverflow.com/q/126374/112
//: Playground - noun: a place where people can play
import UIKit
import Foundation
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
let url = NSURL(string: "http://private-ad72e-jsonsize.apiary-mock.com/exemplo")!
var imagens = [String]()
var links = [String]()
func get() {
NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: {
(data, response, error) -> Void in
// error
if error != nil {
print(error?.localizedDescription)
}
// resposta vazia
guard let data = data else { return }
// parse do JSON
do {
let json = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) as! [[String: AnyObject]]
for objeto in json {
// preenche os arrays com os dados do JSON
imagens.append(String(objeto["imagem"]))
links.append(String(objeto["link"]))
}
} catch {
print("erro no parse do JSON: \(error)")
}
// teste dos arrays
imagens.count //quantidade de itens dentro de imagens
imagens[0] //mostra o objeto 0 de imagens
links.count //quantidade de itens dentro de links
links[1] //mostra o objeto 1 de imagens
XCPlaygroundPage.currentPage.finishExecution()
}).resume()
}
// chama o metodo de teste
get()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment