Error404 Error404 - 26 days ago 8x
Swift Question

How to get the number of rows from a request in tableView method?

I am using a

on my
Swift 3.0
project. I want to get the number of rows of my tableview from a request and I am using
Alamofire 4.0
for that purpose.

I saw that to return values from
you have to set a
(How to return value from Alamofire) but Xcode do not let me set it on

This is the code that I have tried with set/get methods:

var numRows = 0

func setRows(numRows : Int){
self.numRows = numRows

func getRows() -> Int{
return self.numRows

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

var JSON = [[String : AnyObject]]()

Alamofire.request(url!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { response in

switch(response.result) {
case .success(_):
JSON = response.result.value as! [[String : AnyObject]]!
self.setRows(numRows: JSON.count)

case .failure(_):

return self.getRows()

but I am not able to make it work. I know that Alamofire request is asynchronous and is the problem that I am facing but I am not able to set completionHandler on

P.S: I also have tried to make synchronous Alamofire (making an asynchronous alamofire request synchronous) but completion block again appears and I am not able to set it to tableView method (I do not know if it is possible to set it on that method or I am doing it in the wrong way).

I have tried adding
completion:(Int) -> Void
to my tableView method but Xcode does not allow me to do that.

What should I do to set the number of rows after doing the request?

Thanks in advance!

Lu_ Lu_

I'm doing something like this:

table of items that will be in tableView

var positionsTable = [[String : AnyObject]]()

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return positionsTable.count

from request i get items and reload tableView

Alamofire.request(.GET, url, headers: headers, encoding: .JSON)
      .responseJSON { (response) in
          self.positionsTable = response.result.value