Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. //: Playground - noun: a place where people can play
  2.  
  3. import UIKit
  4. import XCPlayground
  5.  
  6. XCPSetExecutionShouldContinueIndefinitely(true)
  7.  
  8. class Contacto : CustomStringConvertible, Hashable, Equatable {
  9. var nombre : String
  10. var telefono : String
  11. var email : String
  12. var id : Int
  13.  
  14. init(id: Int, nombre: String, telefono: String, email: String) {
  15. self.id = id
  16. self.nombre = nombre
  17. self.telefono = telefono
  18. self.email = email
  19. }
  20.  
  21. var description: String {
  22. return "\(nombre) (e-mail:\(email) telefono: \(telefono), id: \(id))";
  23. }
  24.  
  25. var hashValue : Int {
  26. return id
  27. }
  28. }
  29.  
  30. func == (c1: Contacto, c2: Contacto) -> Bool {
  31. return c1.id == c2.id
  32. }
  33.  
  34. func parse(datos: NSDictionary) {
  35. print(datos.description)
  36. }
  37.  
  38. let BASEURL = NSURL(string: "http://www.francisco.chicano.es/AgendaEE-war/rs/agenda/contactos")
  39. let user = "borjatur"
  40. let pass = "riatec2015"
  41.  
  42.  
  43.  
  44. let peticion = NSMutableURLRequest(URL: BASEURL!)
  45. peticion.addValue(user+":"+pass,forHTTPHeaderField: "User-auth")
  46. peticion.addValue("application/json", forHTTPHeaderField: "Accept")
  47. peticion.HTTPMethod = "GET"
  48.  
  49. let sesion = NSURLSession.sharedSession()
  50.  
  51. let dataTask = sesion.dataTaskWithRequest(peticion) {(data,response,error) -> Void in
  52. if (response == nil || data == nil) {
  53. NSLog((error?.description)!)
  54. }
  55. else if (response as! NSHTTPURLResponse).statusCode == 200 {
  56. do {
  57. if let jsonUsuario = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
  58. //convertir NSDictionary en [Contacto]
  59. parse(jsonUsuario)
  60. //callback(arrayContactos)
  61. }
  62. } catch let error as NSError {
  63. NSLog(error.description)
  64. }
  65. }
  66. else {
  67. NSLog("Otro codigo de respuesta")
  68. }
  69. }
  70.  
  71. dataTask.resume()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement