Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.