【问题标题】:Cannot invoke 'getDataInBackgroundWithBlock' with an argument list of type '((NSData!, NSError!) -> Void)'无法使用类型为“((NSData!,NSError!)-> Void)”的参数列表调用“getDataInBackgroundWithBlock”
【发布时间】:2015-04-21 02:25:14
【问题描述】:

我正在使用 swift 6.3 并且有 2 个类似的错误

  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)'

有什么想法吗?

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

【问题讨论】:

  • 从解析头发布 getDataInBackgroundWithBlock 方法的声明。

标签: xcode swift parse-platform


【解决方案1】:

第一个错误是由于块参数错误。 objectserror 都应该是可选的。

如下:

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

第二个原因是一样的。 imageDataerror 也是可选的。

如下:

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

所有固定代码:

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

【讨论】:

  • 非常感谢。现在看起来不错,但是这里显示了其他错误 self.resultsTable.reloadData() 说没有名为“resultsTable”的成员
  • @iAli - 应该是 self.resultTable.reloadData()。有一个错字(resultsTable)。
【解决方案2】:

在 swift 1.2(Xcode 更新到 6.3)中,可选项发生了变化,所以现在 NSError 是可选的。

这使您的功能如下:

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

【讨论】:

    【解决方案3】:

    现在效果很好,其他错误显示在这里

       self.resultsTable.reloadData()
    

    错误UsersVC 没有名为resultsTable 的成员。

    【讨论】:

    • 你在上面定义了resul"t"Table,而不是result"s"Table。
    猜你喜欢
    • 2016-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    • 1970-01-01
    • 2015-12-24
    相关资源
    最近更新 更多