title: Handle Alamofire asynchronous request using SwiftyJSON
body: <p>I am trying to parse <code>JSON</code> data using <code>SwiftyJSON</code> into an array to use in my <code>TableView</code>. However even though I can successfully request the data and parse it into an array, I cannot return it from the <code>getObjects</code> function as it is done asynchronously. I have tried to use a completion handler, and after following several tutorials it seems I am missing something.  </p>
  
  <p>Does anybody know how I can return the array to use in my <code>TableViewController</code> ? </p>
  
  <p><strong>Table View Controller</strong></p>
  
  <pre><code>let objects = [Objects]()
  
  override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(animated)
      let urlString = "URLSTRING"
      objects = dataManager.getObjects(urlString)
      print("objects in view controller products array \(objects.count)")
      self.tableView.reloadData
  }
  </code></pre>
  
  <p><strong>Request Functions</strong></p>
  
  <pre><code>class DataManager {
  
      func requestObjects(_ stringUrl: String, success:@escaping (JSON) -&gt; Void, failure:@escaping (Error) -&gt; Void) {
          print("Request Data")
          Alamofire.request(stringUrl, method: .get).validate().responseJSON { response in
              switch response.result {
                  case .success(let value):
                      let json = JSON(value)
                      print("responce success")
                      success(json)
                  case .failure(let error):
                      print(error)
              } //End of switch statement
          } //End of alamofire request
      }  //End of request function
  
      func getObjects(_ urlString:String) -&gt; [Object] {
          var objects = [Object]()
  
          requestObjects(urlString, success: { (JSONResponse) -&gt; Void in
              let json = JSONResponse
  
              for item in json["items"] {
                  let title = item.1["title"].string 
                  objects.append(Object(title: title!))
              }
              print("Number of objects = \(objects.count)")
          }) {
              (error) -&gt; Void in
              print(error)
          }
          print(objects)  // Prints empty array
          return objects // Array is empty
      }
  }
  </code></pre>