Last active
May 4, 2016 17:09
-
-
Save iTSangarDEV/94aeafe88c3afecf2ace0fcefb06215f to your computer and use it in GitHub Desktop.
Playground para a questão http://pt.stackoverflow.com/q/126374/112
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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