Advertisement
Guest User

Untitled

a guest
May 3rd, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.05 KB | None | 0 0
  1. //
  2. //  ViewController.swift
  3. //  Tableviews
  4. //
  5. //  Created by Dominic Gallo on 5/3/18.
  6. //  Copyright © 2018 Dominic Gallo. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
  12.     var objArr : [jsonObject] = []
  13.    
  14.     internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  15.         return 5
  16.     }
  17.    
  18.     internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  19.         let cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
  20.         cell.textLabel?.text = "Row Number: \(indexPath.row + 1)"
  21.         return cell
  22.     }
  23.     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  24.         performSegue(withIdentifier: "toSecondFromFirst", sender: self)
  25.     }
  26.    
  27.     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  28.        
  29.     }
  30.    
  31.     override func viewDidLoad() {
  32.         super.viewDidLoad()
  33.        
  34.         // Do any additional setup after loading the view, typically from a nib.
  35.         var tempArr : [jsonObject] = []
  36.         var exampleObject = downloadJson(min: 2140, max: 2145) { (jsonDataObject, error) -> [jsonObject] in
  37.             tempArr.append(jsonDataObject!)
  38.             print(tempArr)
  39.             return tempArr
  40.         }
  41.         print(exampleObject)
  42.     }
  43.    
  44.     override func didReceiveMemoryWarning() {
  45.         super.didReceiveMemoryWarning()
  46.         // Dispose of any resources that can be recreated.
  47.     }
  48.    
  49.     func downloadJson(min: Int, max: Int,completionHandler completion : @escaping (jsonObject?, Error?) -> [jsonObject]){
  50.         var tempObject = jsonObject()
  51.         var achievID : Int = min
  52.         for _ in min...max {
  53.             achievID += 1
  54.            
  55.             let url = URL(string: "https://us.api.battle.net/wow/achievement/\(achievID)?locale=en_US&apikey=wh7sqpen5a5gps5sp9mjvp7s9tnrtbsf")
  56.             let urlRequest = URLRequest(url: url!)
  57.             URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in
  58.                 if error != nil {
  59.                     print("error")
  60.                 } else {
  61.                     guard let httpResponse = response as? HTTPURLResponse else {
  62.                         print("Error converting response as http url response or response is nil")
  63.                         return
  64.                     }
  65.                     if httpResponse.statusCode == 200 {
  66.                         guard let dataUnwrapped = data else {
  67.                             print("data is nil ")
  68.                             return
  69.                         }
  70.                         do {
  71.                             tempObject = try JSONDecoder().decode(jsonObject.self, from: dataUnwrapped)
  72.                             completion(tempObject,nil)
  73.                         } catch {
  74.                             print("Error decoding json")
  75.                         }
  76.                     }
  77.                 }
  78.                 }.resume()
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement