Cannot call 'getDataInBackgroundWithBlock' using an argument list of type '((NSData !, NSError!) & # 8594; Void)'

I use swift 6.3 and have 2 identical errors

  • Cannot invoke 'findObjectsInBackgroundWithBlock' with an argument list of type '(([AnyObject]!, NSError!) -> Void)'

  • Cannot invoke 'getDataInBackgroundWithBlock' with an argument list of type '((NSData!, NSError!) -> Void)'

Any ideas please?

import Parse
import UIKit

class UserVC: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet var resultTable: UITableView!
    var resultNameArray = [String]()
    var resultUserNameArray = [String]()
    var resultUserImageFiles = [PFFile]()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func viewDidAppear(animated: Bool) {
        resultNameArray.removeAll(keepCapacity: false)
        resultUserNameArray.removeAll(keepCapacity: false)
        resultUserImageFiles.removeAll(keepCapacity: false)

        var query = PFUser.query()

        query!.whereKey("username", notEqualTo: PFUser.currentUser()!.username!)

        //// here is the error////
        query.findObjectsInBackgroundWithBlock {(objects:[AnyObject]!, error:NSError!) -> Void in
            if error == nil {
                for object in objects {
                    self.resultNameArray.append(object.objectForKey("profileName") as String)
                    self.resultUserImageFiles.append(object.objectForKey("photo") as PFFile)
                    self.resultUserNameArray.append(object.objectForKey("username") as String)

                    self.resultsTable.reloadData()
                }
            }
        }
    }

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

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 64
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell:User_Cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! User_Cell
        cell.profileLbl.text = self.resultNameArray[indexPath.row]
        cell.userLbl.text = self.resultUserNameArray[indexPath.row]

        //// here is the error////
        self.resultUserImageFiles[indexPath.row].getDataInBackgroundWithBlock {
            (imageData:NSData!, error:NSError!) -> Void in

            if error == nil {
                let image = UIImage(data: imageData)
                cell.imgView.image = image

            }
        }

        return cell
    }
}
+4
source share
3 answers

The first error is due to incorrect block parameters. objectsand errorboth must be optional.

As below:

query?.findObjectsInBackgroundWithBlock({ (objects: [AnyObject]?, error: NSError?) -> Void in

The second reason. imageDataand errorare also optional.

As below:

self.resultUserImageFiles[indexPath.row].getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in

All fixed code:

class UserVC: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet var resultTable: UITableView!
    var resultNameArray = [String]()
    var resultUserNameArray = [String]()
    var resultUserImageFiles = [PFFile]()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func viewDidAppear(animated: Bool) {
        resultNameArray.removeAll(keepCapacity: false)
        resultUserNameArray.removeAll(keepCapacity: false)
        resultUserImageFiles.removeAll(keepCapacity: false)

        var query = PFUser.query()

        query!.whereKey("username", notEqualTo: PFUser.currentUser()!.username!)

        //// here is the error////
        query?.findObjectsInBackgroundWithBlock({ (objects: [AnyObject]?, error: NSError?) -> Void in
            if error == nil {
                if let objects = objects {
                    for object in objects {
                        self.resultNameArray.append(object.objectForKey("profileName") as! String)
                        self.resultUserImageFiles.append(object.objectForKey("photo") as! PFFile)
                        self.resultUserNameArray.append(object.objectForKey("username") as! String)

                        self.resultsTable.reloadData()
                    }
                }
            }
        })
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return resultNameArray.count
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 64
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell:User_Cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! User_Cell
        cell.profileLbl.text = self.resultNameArray[indexPath.row]
        cell.userLbl.text = self.resultUserNameArray[indexPath.row]

        //// here is the error////
        self.resultUserImageFiles[indexPath.row].getDataInBackgroundWithBlock { (imageData: NSData?, error:NSError?) -> Void in
            if error == nil {
                let image = UIImage(data: imageData!)
                cell.imgView.image = image
            }
        }

        return cell
    }
}
+7
source

The parameters swift 1.2 (Xcode update to 6.3) have been changed, so now NSError is optional.

What makes your function as follows:

query?.findObjectsInBackgroundWithBlock({ (objects: [AnyObject]?, error: NSError?) -> Void in 
    // Do whatever you want with these objects
})
+1

Now it works fine, and another error appears here.

   self.resultsTable.reloadData()

the error UsersVCdoes not have a member with a name resultsTable.

-2
source

All Articles