Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func generateUser() {
- self.IDName.stringValue = "..."
- self.IDStreet.stringValue = "..."
- self.IDCity.stringValue = "..."
- self.IDMail.stringValue = "..."
- self.IDPhone.stringValue = "..."
- self.IDFirstname.stringValue = "..."
- self.IDUsername.stringValue = "..."
- self.IDPassword.stringValue = "..."
- self.IDNationality.stringValue = "..."
- let requestURL: NSURL = NSURL(string: "https://randomuser.me/api")! // API URL: A free, open-source API for generating random user data.
- let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL)
- let session = NSURLSession.sharedSession() // Singleton session based on default configuration
- let task = session.dataTaskWithRequest(urlRequest) {
- (data, response, error) -> Void in
- let httpResponse = response as! NSHTTPURLResponse
- let statusCode = httpResponse.statusCode // The receiver’s HTTP status code.
- if (statusCode == 200) {
- print("File download successfull.")
- let getData: NSData = data!
- let json = JSON(data: getData) // Retrieving data from the JSON file
- let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
- dispatch_async(dispatch_get_global_queue(priority, 0)) {
- dispatch_async(dispatch_get_main_queue()) {
- if var lastname = json["results"][0]["name"]["last"].string {
- lastname.replaceRange(lastname.startIndex...lastname.startIndex, with: String(lastname[lastname.startIndex]).capitalizedString) // Capitalize only the first letter
- self.IDName.stringValue = lastname // Label takes variable's value
- print(lastname)
- }
- if var firstname = json["results"][0]["name"]["first"].string {
- firstname.replaceRange(firstname.startIndex...firstname.startIndex, with: String(firstname[firstname.startIndex]).capitalizedString) // Capitalize only the first letter
- self.IDFirstname.stringValue = firstname
- print(firstname)
- }
- if let nationality = json["results"][0]["nat"].string {
- self.IDNationality.stringValue = nationality
- print(nationality)
- }
- if let street = json["results"][0]["location"]["street"].string {
- self.IDStreet.stringValue = street
- print(street)
- }
- if var city = json["results"][0]["location"]["city"].string {
- city.replaceRange(city.startIndex...city.startIndex, with: String(city[city.startIndex]).capitalizedString) // Capitalize only the first letter
- self.IDCity.stringValue = city
- print(city)
- }
- if let mail = json["results"][0]["email"].string {
- self.IDMail.stringValue = mail
- print(mail)
- }
- if let phone = json["results"][0]["phone"].string {
- self.IDPhone.stringValue = phone
- print(phone)
- }
- if let username = json["results"][0]["login"]["username"].string {
- self.IDUsername.stringValue = username
- print(username)
- }
- if let password = json["results"][0]["login"]["password"].string {
- self.IDPassword.stringValue = password
- print(self.IDPassword.stringValue)
- }
- if let picture = json["results"][0]["picture"]["large"].string {
- print(picture)
- let pictureURL = NSURL(string: picture) // Image link
- if let pictureData = NSData(contentsOfURL: pictureURL!){
- self.IDPicture.image = NSImage(data: pictureData)
- }
- }
- else {
- print("Il n'y a pas de personne disponible")
- }
- }
- }
- }
- }
- task.resume() // Execute this task
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement