Search code examples
swiftparsekit

Cannot invoke 'getDataInBackground' with an argument list of type '((NSData?, NSError?) -> Void) error


I'm getting the following error: Cannot invoke 'getDataInBackground' with an argument list of type '((NSData?, NSError?) -> Void)'

I found similar question, but as you can see the code does'nt work for me, here my code:

import UIKit
import Parse
.
.
.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell: resultsCell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! resultsCell
    cell.usernameLbl.text = self.resultsUsernameArray[indexPath.row]
    cell.profileNameLbl.text = self.resultsProfileNameArray[indexPath.row]   
    //here error in line below:  
    self.resultsImageFiles[indexPath.row].getDataInBackground { (imageData: NSData?, error: NSError?) -> Void in   
        if error == nil{
            let image = UIImage(data: imageData)
            cell.profileImg.image = image
        }

    }
    return cell
}

Solution

  • You can avoid these problems in Swift 3+ if you simply omit the types in the closure

    self.resultsImageFiles[indexPath.row].getDataInBackground { (imageData, error) in
    

    In Swift 3+ NSData is replaced with Data and NSError with Error

    Alternatively comment out the line, retype it and use code completion.

    Apart from the issue you are discouraged to run unmanaged asynchronous tasks in cellForRow because the cell can be deallocated immediately.

    And – as you are new with swift – please conform to the naming convention that struct and class names start with a capital letter (ResultsCell).