解析查询无法在Swift 2.0

时间:2016-01-26 15:06:07

标签: ios swift parse-platform

所以我有一个试图在Parse中查找数据库中的用户的查询。出于某种原因,在更新到Swift 2.0后,我收到以下代码的以下错误。

  

无法转换类型'([AnyObject]?,NSError?) - >的值无效的   期望参数类型'PFQueryArrayResultBlock?'

这是给我错误的代码,我不知道为什么。整个功能发布在下面。

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

如果有任何帮助,这就是整个功能。

func searchBarSearchButtonClicked(searchBar: UISearchBar) {

    searchBar.resignFirstResponder()

    let fullnameQuery = PFQuery(className: "_User")
    fullnameQuery.whereKey("fullname", containsString: searchBar.text)

    let usernameQuery = PFQuery(className: "_User")
    usernameQuery.whereKey("username", containsString: searchBar.text)

    let query = PFQuery.orQueryWithSubqueries([fullnameQuery, usernameQuery])

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

         if error != nil {

             let myAlert = UIAlertController(title: "Alert", message: error?.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)

            let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)

            myAlert.addAction(okAction)
            self.presentViewController(myAlert, animated: true, completion: nil)
            return
         }

         if let objects = results as? [PFObject] {

            self.searchResults.removeAll(keepCapacity: false)

            for object in objects {

                self.searchResults.append(object)

            }

            dispatch_async(dispatch_get_main_queue()) {
                self.table.reloadData()
                self.searchController.resignFirstResponder()
           }
        }
    }

}

3 个答案:

答案 0 :(得分:2)

要正确调用此功能,您需要使用以下

        query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
        if let objects = objects {
            if error == nil {
                for object in objects {
                    self.searchResults.append(object)
                }

                dispatch_async(dispatch_get_main_queue()) {
                    self.table.reloadData()
                    self.searchController.resignFirstResponder()
                }     
            } else {
                print("Error: \(error!) \(error!.userInfo)")
            }
        }
    }

答案 1 :(得分:1)

更好的是,使用swift,编译器非常适合根据方法签名推断类型。所有你真正需要的是:

query.findObjectsInBackgroundWithBlock {
    (results, error ) -> Void in

}

答案 2 :(得分:0)

尝试将其更改为

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