Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
854 views
in Technique[技术] by (71.8m points)

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

I'm using swift 6.3 and having 2 similar errors

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

  2. 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
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The first error is due to block parameters are wrong. objects and error both should be optional.

Like below:

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

Second is same reason. imageData and error also both optional.

Like 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
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...