Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // Tableviews
- //
- // Created by Dominic Gallo on 5/3/18.
- // Copyright © 2018 Dominic Gallo. All rights reserved.
- //
- import UIKit
- class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
- var objArr : [jsonObject] = []
- internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return 5
- }
- internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
- cell.textLabel?.text = "Row Number: \(indexPath.row + 1)"
- return cell
- }
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- performSegue(withIdentifier: "toSecondFromFirst", sender: self)
- }
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view, typically from a nib.
- var tempArr : [jsonObject] = []
- var exampleObject = downloadJson(min: 2140, max: 2145) { (jsonDataObject, error) -> [jsonObject] in
- tempArr.append(jsonDataObject!)
- print(tempArr)
- return tempArr
- }
- print(exampleObject)
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- func downloadJson(min: Int, max: Int,completionHandler completion : @escaping (jsonObject?, Error?) -> [jsonObject]){
- var tempObject = jsonObject()
- var achievID : Int = min
- for _ in min...max {
- achievID += 1
- let url = URL(string: "https://us.api.battle.net/wow/achievement/\(achievID)?locale=en_US&apikey=wh7sqpen5a5gps5sp9mjvp7s9tnrtbsf")
- let urlRequest = URLRequest(url: url!)
- URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in
- if error != nil {
- print("error")
- } else {
- guard let httpResponse = response as? HTTPURLResponse else {
- print("Error converting response as http url response or response is nil")
- return
- }
- if httpResponse.statusCode == 200 {
- guard let dataUnwrapped = data else {
- print("data is nil ")
- return
- }
- do {
- tempObject = try JSONDecoder().decode(jsonObject.self, from: dataUnwrapped)
- completion(tempObject,nil)
- } catch {
- print("Error decoding json")
- }
- }
- }
- }.resume()
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement