无法使用类型为 '((NSData!, NSError!) -> Void)' 的参数列表调用 'getDataInBackgroundWithBlock' [英] Cannot invoke 'getDataInBackgroundWithBlock' with an argument list of type '((NSData!, NSError!) -> Void)'

查看:32
本文介绍了无法使用类型为 '((NSData!, NSError!) -> Void)' 的参数列表调用 'getDataInBackgroundWithBlock'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using swift 6.3 and having 2 similar errors

  1. 无法使用类型为 '(([AnyObject]!, NSError!) -> Void)' 的参数列表调用 'findObjectsInBackgroundWithBlock'

无法使用类型为 '(((NSData!, NSError!) -> Void)' 的参数列表调用 'getDataInBackgroundWithBlock'

有什么想法吗?

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

推荐答案

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

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

如下图:

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

第二个是同样的原因.imageDataerror 也是可选的.

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

如下图:

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

这篇关于无法使用类型为 '((NSData!, NSError!) -> Void)' 的参数列表调用 'getDataInBackgroundWithBlock'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆