title: Convert UNIX time from json import (swift struct) to date as string and populate table
body: <p>I have a json file being imported to my project (<a href="https://api.myjson.com/bins/ywv0k" rel="nofollow noreferrer">https://api.myjson.com/bins/ywv0k</a>). The json attributes are decoded and stored in my struct class "News", which has the same attributes like the json file.</p>
  
  <p>in a second step I populate a table with the string attribute "timestamp" from my struct class "News", which is actually a UNIX time.</p>
  
  <p>My problem now is that I am lost how to change this UNIX time to a string of format "dd/mm/yy HH:mm:ss", since I get an error when I try to put a function </p>
  
  <pre><code>let date = NSDate(timeIntervalSince1970: timestamp) //error since timestamp is currently defined as string. If I make it a long variable, I cannot populate the table with it any more, since the label requires a text with string format.
  
  let dayTimePeriodFormatter = NSDateFormatter()
  dayTimePeriodFormatter.dateFormat = "dd/mm/yy HH:mm:ss"
  
   let dateString = dayTimePeriodFormatter.stringFromDate(date)
  </code></pre>
  
  <p>into the do-encoding-loop as well as when I put it into this table function: func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell.</p>
  
  <p><strong>Swift 4</strong></p>
  
  <pre><code>import UIKit
  
  // structure from json file
  struct News: Codable{
      let type: String
      let timestamp: String // UNIX format, eg. "1531294146340"
      let title: String
      let message: String
  }
  
  class HomeVC: BaseViewController, UITableViewDelegate, UITableViewDataSource {
      var myNewsItems: [News] = []
      @IBOutlet weak var myNewTableView: UITableView!   
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          let nibName = UINib(nibName: "CustomTableViewCell", bundle: nil)
          myNewTableView.register(nibName, forCellReuseIdentifier: "tableViewCell")
  
  
  // JSON Decoding
  
          let url=URL(string:"https://api.myjson.com/bins/ywv0k")
          let session = URLSession.shared
          let task = session.dataTask(with: url!) { (data, response, error) in
  
              guard let data = data else { return }
  
              do {
                  let myNewsS = try
                      JSONDecoder().decode([News].self, from: data)
                  print(myNewsS)
  
                  self.myNewsItems = myNewsS
                  DispatchQueue.main.async {
                      self.myNewTableView.reloadData()
                  }
              } catch let jsonErr {
              }
  
          }
          task.resume()
      }
  
      func numberOfSections(in tableView: UITableView) -&gt; Int {
          return 1
      }
  
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return myNewsItems.count
      }
  
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as!
  CustomTableViewCell
  
  // populate table with json content
          cell.commonInit(timestamp: myNewsItems[indexPath.row].timestamp, message: myNewsItems[indexPath.row].message)
  
          return cell
      }
  
      func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
              cell.backgroundColor = UIColor(white: 1, alpha: 0.5)
      }
  }
  </code></pre>