Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. func generateUser() {
  2. self.IDName.stringValue = "..."
  3. self.IDStreet.stringValue = "..."
  4. self.IDCity.stringValue = "..."
  5. self.IDMail.stringValue = "..."
  6. self.IDPhone.stringValue = "..."
  7. self.IDFirstname.stringValue = "..."
  8. self.IDUsername.stringValue = "..."
  9. self.IDPassword.stringValue = "..."
  10. self.IDNationality.stringValue = "..."
  11.  
  12.  
  13. let requestURL: NSURL = NSURL(string: "https://randomuser.me/api")! // API URL: A free, open-source API for generating random user data.
  14. let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL)
  15. let session = NSURLSession.sharedSession() // Singleton session based on default configuration
  16. let task = session.dataTaskWithRequest(urlRequest) {
  17. (data, response, error) -> Void in
  18.  
  19. let httpResponse = response as! NSHTTPURLResponse
  20. let statusCode = httpResponse.statusCode // The receiver’s HTTP status code.
  21.  
  22. if (statusCode == 200) {
  23. print("File download successfull.")
  24.  
  25. let getData: NSData = data!
  26.  
  27. let json = JSON(data: getData) // Retrieving data from the JSON file
  28.  
  29. let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
  30. dispatch_async(dispatch_get_global_queue(priority, 0)) {
  31. dispatch_async(dispatch_get_main_queue()) {
  32. if var lastname = json["results"][0]["name"]["last"].string {
  33. lastname.replaceRange(lastname.startIndex...lastname.startIndex, with: String(lastname[lastname.startIndex]).capitalizedString) // Capitalize only the first letter
  34. self.IDName.stringValue = lastname // Label takes variable's value
  35. print(lastname)
  36. }
  37. if var firstname = json["results"][0]["name"]["first"].string {
  38. firstname.replaceRange(firstname.startIndex...firstname.startIndex, with: String(firstname[firstname.startIndex]).capitalizedString) // Capitalize only the first letter
  39. self.IDFirstname.stringValue = firstname
  40. print(firstname)
  41. }
  42. if let nationality = json["results"][0]["nat"].string {
  43. self.IDNationality.stringValue = nationality
  44. print(nationality)
  45. }
  46. if let street = json["results"][0]["location"]["street"].string {
  47. self.IDStreet.stringValue = street
  48. print(street)
  49. }
  50. if var city = json["results"][0]["location"]["city"].string {
  51. city.replaceRange(city.startIndex...city.startIndex, with: String(city[city.startIndex]).capitalizedString) // Capitalize only the first letter
  52. self.IDCity.stringValue = city
  53. print(city)
  54. }
  55. if let mail = json["results"][0]["email"].string {
  56. self.IDMail.stringValue = mail
  57. print(mail)
  58. }
  59. if let phone = json["results"][0]["phone"].string {
  60. self.IDPhone.stringValue = phone
  61. print(phone)
  62. }
  63. if let username = json["results"][0]["login"]["username"].string {
  64. self.IDUsername.stringValue = username
  65. print(username)
  66. }
  67. if let password = json["results"][0]["login"]["password"].string {
  68. self.IDPassword.stringValue = password
  69. print(self.IDPassword.stringValue)
  70. }
  71. if let picture = json["results"][0]["picture"]["large"].string {
  72. print(picture)
  73. let pictureURL = NSURL(string: picture) // Image link
  74. if let pictureData = NSData(contentsOfURL: pictureURL!){
  75. self.IDPicture.image = NSImage(data: pictureData)
  76. }
  77. }
  78. else {
  79. print("Il n'y a pas de personne disponible")
  80. }
  81. }
  82. }
  83. }
  84. }
  85.  
  86. task.resume() // Execute this task
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement