Bardia Modaberi
Bardia Modaberi

Reputation: 5

Returning value from a Parse function in swift

I want to return a value from a parse function in swift but I'm running into a problem... When I try to return the value in the function I get "Cannot convert value of type 'Int' to closure result type '()'"

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    let query = PFQuery(className: "people")
    query.countObjectsInBackground
    { (count, error) in
        
        return Int(count)
    }
}

Upvotes: 0

Views: 92

Answers (1)

Mat
Mat

Reputation: 6324

You are returning a closure and numberOfRowsInSection takes an Int.

I am not really sure what is the logic behind but what you can do is:

// declare this variable 
var numberOfSections:Int?

// then inside a function or viewDidLoad
let query = PFQuery(className: "people")
query.countObjectsInBackground
{ (count, error) in
    self.numberOfSections = Int(count)
    // finally you force the tableView to reload
    DispatchQueue.main.async {
        self.tableView.reloadData()
    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
   return numberOfSections ?? 0 
  // maybe you want to return at least one section when 
 //  you var is nil so can can change it to ?? 1
}

Upvotes: 1

Related Questions