Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class ViewControllerProfile: UIViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view, typically from a nib.
- //test to see if we got the name from the segue:
- print("from Segue: " + summonerName)
- //execute on view load:
- let theUser = Summoner(name: summonerName)
- print(theUser.name!)
- //API - Summoners Details By Summoner Name:
- //Construct request for name in secondViewController(PROFILE)
- let root:String = "https://euw1.api.riotgames.com"
- let entryPoint:String = "/lol/summoner/v3/summoners/by-name/"
- let key:String = "?api_key=RGAPI-728b9775-cfdf-4065-acca-70cb732aed0d"
- //theUser.name! is a search term (players name)
- let completeURL = root + entryPoint + theUser.name! + key
- let urlRecieved = URL(string: completeURL.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!)
- let task = URLSession.shared.dataTask(with: urlRecieved!){ (data, response, error) in
- if error != nil{
- print("ERROR")
- }
- else{
- if let content = data{
- do{
- //Array:
- let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String:Any]
- //We now extract the required information from the JSON output with keys:
- //Class generics for future parsing:
- let summonerID = myJson["id"] as? UInt64
- let usersSummonerID:Int = Int(summonerID!)
- print(usersSummonerID)
- let accountID = myJson["accountId"] as? UInt64
- let usersAccountID = Int(accountID!)
- print(usersAccountID)
- //Required elements for Profile Labels:
- let extractName = myJson["name"] as? String
- let extractLevel = myJson["summonerLevel"] as? UInt64
- //We need to convert the extractLevel variable from UInt64 to String to update a label without an optional:
- let usersLevel:String = "(extractLevel!)"
- //We update the gui now using the data we got from the API call:
- DispatchQueue.main.async {
- //We dispatch the user interface updates to the main thread here:
- self.summonerLevelLbl.text = ("Level: " + "(usersLevel)")
- self.summonerNameLbl.text = extractName
- //We dispatch the api-returned userObject's attributes to the main thread for assignment here:
- self.summonerNameMain = extractName!
- self.summonerIDMain = usersSummonerID
- self.accountIDMain = usersAccountID
- print("dispatch completednn")
- }
- }
- catch{
- print("SOMETHING WENT WRONG WITH SERIALIZATION OF NAME X")
- }
- }
- }
- }
- task.resume()
- theUser.name = summonerNameMain
- theUser.accountID = accountIDMain
- theUser.summonerID = summonerIDMain
- print("ASSIGNMENT TEST")
- print(theUser.name!)
- print(theUser.summonerID!)
- print(theUser.accountID!)
- }
- @IBOutlet weak var summonerNameLbl: UILabel!
- @IBOutlet weak var summonerRankLbl: UILabel!
- @IBOutlet weak var summonerLevelLbl: UILabel!
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- //For Segue:
- var summonerName = String()
- //For Parsing:
- var accountIDMain = Int()
- var summonerIDMain = Int()
- var summonerNameMain = String()
- }
- class Summoner:NSObject{
- var name:String?
- var summonerID:Int?
- var accountID:Int?
- var matchID:Int?
- init(name: String){
- self.name = name
- }
- }
Add Comment
Please, Sign In to add comment