Guest User

Untitled

a guest
Nov 18th, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. import UIKit
  2.  
  3. class ViewControllerProfile: UIViewController {
  4.  
  5. override func viewDidLoad() {
  6. super.viewDidLoad()
  7. // Do any additional setup after loading the view, typically from a nib.
  8.  
  9. //test to see if we got the name from the segue:
  10. print("from Segue: " + summonerName)
  11.  
  12. //execute on view load:
  13. let theUser = Summoner(name: summonerName)
  14. print(theUser.name!)
  15.  
  16.  
  17.  
  18.  
  19. //API - Summoners Details By Summoner Name:
  20.  
  21.  
  22. //Construct request for name in secondViewController(PROFILE)
  23. let root:String = "https://euw1.api.riotgames.com"
  24. let entryPoint:String = "/lol/summoner/v3/summoners/by-name/"
  25. let key:String = "?api_key=RGAPI-728b9775-cfdf-4065-acca-70cb732aed0d"
  26. //theUser.name! is a search term (players name)
  27. let completeURL = root + entryPoint + theUser.name! + key
  28. let urlRecieved = URL(string: completeURL.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!)
  29.  
  30.  
  31.  
  32. let task = URLSession.shared.dataTask(with: urlRecieved!){ (data, response, error) in
  33. if error != nil{
  34. print("ERROR")
  35. }
  36. else{
  37. if let content = data{
  38. do{
  39. //Array:
  40. let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String:Any]
  41.  
  42. //We now extract the required information from the JSON output with keys:
  43. //Class generics for future parsing:
  44. let summonerID = myJson["id"] as? UInt64
  45. let usersSummonerID:Int = Int(summonerID!)
  46. print(usersSummonerID)
  47.  
  48. let accountID = myJson["accountId"] as? UInt64
  49. let usersAccountID = Int(accountID!)
  50. print(usersAccountID)
  51.  
  52. //Required elements for Profile Labels:
  53. let extractName = myJson["name"] as? String
  54. let extractLevel = myJson["summonerLevel"] as? UInt64
  55. //We need to convert the extractLevel variable from UInt64 to String to update a label without an optional:
  56. let usersLevel:String = "(extractLevel!)"
  57.  
  58. //We update the gui now using the data we got from the API call:
  59. DispatchQueue.main.async {
  60. //We dispatch the user interface updates to the main thread here:
  61. self.summonerLevelLbl.text = ("Level: " + "(usersLevel)")
  62. self.summonerNameLbl.text = extractName
  63.  
  64. //We dispatch the api-returned userObject's attributes to the main thread for assignment here:
  65. self.summonerNameMain = extractName!
  66. self.summonerIDMain = usersSummonerID
  67. self.accountIDMain = usersAccountID
  68.  
  69. print("dispatch completednn")
  70.  
  71. }
  72.  
  73.  
  74. }
  75.  
  76. catch{
  77. print("SOMETHING WENT WRONG WITH SERIALIZATION OF NAME X")
  78. }
  79. }
  80. }
  81. }
  82. task.resume()
  83.  
  84.  
  85.  
  86.  
  87. theUser.name = summonerNameMain
  88. theUser.accountID = accountIDMain
  89. theUser.summonerID = summonerIDMain
  90.  
  91.  
  92. print("ASSIGNMENT TEST")
  93. print(theUser.name!)
  94. print(theUser.summonerID!)
  95. print(theUser.accountID!)
  96. }
  97.  
  98. @IBOutlet weak var summonerNameLbl: UILabel!
  99.  
  100. @IBOutlet weak var summonerRankLbl: UILabel!
  101.  
  102. @IBOutlet weak var summonerLevelLbl: UILabel!
  103.  
  104.  
  105.  
  106.  
  107. override func didReceiveMemoryWarning() {
  108. super.didReceiveMemoryWarning()
  109. // Dispose of any resources that can be recreated.
  110. }
  111.  
  112. //For Segue:
  113. var summonerName = String()
  114.  
  115. //For Parsing:
  116. var accountIDMain = Int()
  117. var summonerIDMain = Int()
  118. var summonerNameMain = String()
  119.  
  120.  
  121.  
  122.  
  123. }
  124.  
  125. class Summoner:NSObject{
  126.  
  127. var name:String?
  128. var summonerID:Int?
  129. var accountID:Int?
  130. var matchID:Int?
  131.  
  132. init(name: String){
  133. self.name = name
  134.  
  135. }
  136.  
  137. }
Add Comment
Please, Sign In to add comment