title: Swift 4 - URLSession
body: <p>I have a small issue when parsing APIs from the website www.adx.ae</p>
  
  <p>Here are the two APIs:</p>
  
  <p>1- List of all public companies: <a href="https://www.adx.ae/en/_vti_bin/adx/svc/Members.svc/ListedCompanies?showPrivate=false&amp;showPublic=true" rel="nofollow noreferrer">https://www.adx.ae/en/_vti_bin/adx/svc/Members.svc/ListedCompanies?showPrivate=false&amp;showPublic=true</a></p>
  
  <p>2- Detail of each company by its ID:
  <a href="https://www.adx.ae/en/_vti_bin/ADX/svc/trading.svc/ListedCompanyOrderBook?listedCompanyID=ADIB" rel="nofollow noreferrer">https://www.adx.ae/en/_vti_bin/ADX/svc/trading.svc/ListedCompanyOrderBook?listedCompanyID=ADIB</a></p>
  
  <p>All we have to do is to chance the ID according to each company to get its latest trading price ... etc.</p>
  
  <p>My aim here is to list both the names and each company's trading price in a tableView. </p>
  
  <p>So far here is what I have done</p>
  
  <pre><code>import UIKit
  
  class ViewController: UITableViewController {
  
      func getPrice(company: [Company]) {
  
          var price = [String]()
          let companyLink = "https://www.adx.ae/en/_vti_bin/ADX/svc/trading.svc/ListedCompanyOrderBook?listedCompanyID="
  
          for comp in company {
              let link = companyLink + comp.ID
  
              guard let url = URL(string: link) else { return }
  
              URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in
  
                  guard let dataResult = data else { return }
  
                  do {
  
                      let company = try JSONDecoder().decode(SpecificCompany.self, from: dataResult)
  
                      DispatchQueue.main.async {
                          price.append(String(company.LastTradePrice))
                          self.tableView.reloadData()
  
                      }
  
                  } catch {
                      print(error)
                  }
              }).resume()
          }
  
          print(price)
          allPrices = price
  
      }
  
      struct Company: Decodable {
          let Name: String
          let ID: String
          var link: String {
              get {
                  return "https://www.adx.ae/en/_vti_bin/ADX/svc/trading.svc/ListedCompanyOrderBook?listedCompanyID=" + ID
                  }
          }
  
      }
  
      struct SpecificCompany: Decodable {
          let LastTradePrice: Double
      }
  
      var allCompanies = [Company]()
      var allPrices = [String]()
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          getCompanies()
      }
  
      func getCompanies() {
  
          let link = "https://www.adx.ae/en/_vti_bin/adx/svc/Members.svc/ListedCompanies?showPrivate=false&amp;showPublic=true"
  
          guard let url = URL(string: link) else { return print("Error") }
  
          URLSession.shared.dataTask(with: url) { (data, response, error) in
  
              guard let obtainedData = data else { return print("No data")}
  
              do {
                  let adx = try JSONDecoder().decode([Company].self, from: obtainedData)
  
                  DispatchQueue.main.async {
                      self.allCompanies = adx
                      self.tableView.reloadData()
                  }
  
              } catch {
                  print("Nothing")
                  print(error)
              }
  
              }.resume()
  
      }
  
      override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return allCompanies.count
      }
  
      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
          cell.textLabel?.text = allCompanies[indexPath.row].ID
          cell.detailTextLabel?.text = allPrices[indexPath.row]
          return cell
      }
  }
  </code></pre>
  
  <p>I am not able to get the prices in the array that I am looking for. The allCompanies array seems to be empty until the function of cellForRowAt indexPath is called!! Even putting the function getPrice there is not solving the issue.</p>
  
  <p>Am I missing something here ?</p>