title: Show data in table view from mysql using alamofire and swiftyjson
body: <p>I have a php script on my server which is just a basic sql SELECT statement which gets some data from a mysql database and it returns a number of rows. </p>
  
  <p>I have used alamofire and swiftyjson to print the data out to console but I want to show it in a table view. </p>
  
  <p>Due to the call not being in the same scope as the tableView code ( I think that is the reason I am getting an error which says 'Use of unresolved idenfifier') </p>
  
  <p>I am not sure how to make it global but I guess I need to create a global array variable but not sure if it should just be empty?</p>
  
  <p>global:</p>
  
  <pre><code> let serviceURL = "http://example.com/service.php"
  </code></pre>
  
  <p>I put it in a function like this:</p>
  
  <pre><code>    func getUsers() {
  
      Alamofire.request(serviceURL, method: .get).validate().responseJSON { (response) in
  
          if response.result.isSuccess {
  
              let userJSON : JSON = JSON(response.result.value!)
  
  
              for (index,subJson):(String, JSON) in userJSON  {
                  let firstName = subJson["first_name"].string 
                  let lastName = subJson["last_name"].string
                  print(firstName)
              }
  
          } else {
  
              print("Could not get results")
          }
      }
  }
  </code></pre>
  
  <p>I need to somehow count the rows returned</p>
  
  <pre><code>override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
  
     return firstName.count
  
  }
  </code></pre>
  
  <p>And then actually display in the cell</p>
  
  <pre><code>override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "userCell", for: indexPath)
  
      cell.textLabel?.text = names[indexPath.row]
  
      return cell
  }
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <pre><code>import UIKit
  import Alamofire
  import SwiftyJSON
  
  struct User {
      var firstName: String
      var lastName: String
  
      private enum CodingKeys: String, CodingKey {
  
          case firstName = "first_name"
          case lastName = "last_name"
      }
  }
  
  class UserTableViewController: UITableViewController {
  
      var users = [User]()
  
      let serviceURL = "http://example.com/service.php"
  
  
      override func viewDidLoad() {
          super.viewDidLoad()
          getUsers()
  
      }
  
  
      func getUsers() {
          Alamofire.request(serviceURL, method: .get).validate().responseJSON { (response) in
              if response.result.isSuccess {
                  let userJSON : JSON = JSON(response.result.value!)
  
                  for (index,subJson):(String, JSON) in userJSON  {
                      let firstName = subJson["first_name"].string
                      let lastName = subJson["last_name"].string
                      let user = User(firstName: firstName!, lastName: lastName!)
                      self.users.append(user)
  
                  }
              } else {
                  print("Could not get results")
              }
          }
      }
  
      override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return users.count
      }
  
      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "userCell", for: indexPath)
          let user = users[indexPath.row]
          cell.textLabel?.text = "\(user.firstName) \(user.lastName)"
  
          return cell
      }
  }
  </code></pre>