Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //: Playground - noun: a place where people can play
- import UIKit
- import XCPlayground
- XCPSetExecutionShouldContinueIndefinitely(true)
- class Contacto : CustomStringConvertible, Hashable, Equatable {
- var nombre : String
- var telefono : String
- var email : String
- var id : Int
- init(id: Int, nombre: String, telefono: String, email: String) {
- self.id = id
- self.nombre = nombre
- self.telefono = telefono
- self.email = email
- }
- var description: String {
- return "\(nombre) (e-mail:\(email) telefono: \(telefono), id: \(id))";
- }
- var hashValue : Int {
- return id
- }
- }
- func == (c1: Contacto, c2: Contacto) -> Bool {
- return c1.id == c2.id
- }
- func parse(datos: NSDictionary) {
- print(datos.description)
- }
- let BASEURL = NSURL(string: "http://www.francisco.chicano.es/AgendaEE-war/rs/agenda/contactos")
- let user = "borjatur"
- let pass = "riatec2015"
- let peticion = NSMutableURLRequest(URL: BASEURL!)
- peticion.addValue(user+":"+pass,forHTTPHeaderField: "User-auth")
- peticion.addValue("application/json", forHTTPHeaderField: "Accept")
- peticion.HTTPMethod = "GET"
- let sesion = NSURLSession.sharedSession()
- let dataTask = sesion.dataTaskWithRequest(peticion) {(data,response,error) -> Void in
- if (response == nil || data == nil) {
- NSLog((error?.description)!)
- }
- else if (response as! NSHTTPURLResponse).statusCode == 200 {
- do {
- if let jsonUsuario = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
- //convertir NSDictionary en [Contacto]
- parse(jsonUsuario)
- //callback(arrayContactos)
- }
- } catch let error as NSError {
- NSLog(error.description)
- }
- }
- else {
- NSLog("Otro codigo de respuesta")
- }
- }
- dataTask.resume()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement