当前位置: 首页 > 知识库问答 >
问题:

Swift:无法将类型“()->bool”的值转换为预期的参数类型“pfobject”

公西俊德
2023-03-14

在总体方案中,我试图比较用户从tableview中的多个选择,并将它们与我的解析数据库进行比较。所以我的问题是两倍1。我当前的代码是正确的方式吗?和2。如何将值类型Bool转换为参数类型pfobject?

无法将类型“()->bool”的值转换为预期的参数类型“pfobject”

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {


    if segue.identifier == "showResults" {

        // Get reference to destination view controller
        let destination = segue.destinationViewController as! CollectionViewController

        if let selectedItem = tableView.indexPathsForSelectedRows{

            for var i = 0; i < selectedItem.count; ++i {

                var currentPath = selectedItem[i] as NSIndexPath
                var cell = tableView.cellForRowAtIndexPath(currentPath)

                if let cell = cell {

                    //add major(s) selected to data variable in collectionview as type text(String)
                    destination.data.append(cell.textLabel!.text!)

                }

让imagesQuery=PFQuery(classname:“CollegeImages”)imagesQuery.SelectKeys([“name”])imagesQuery.FindObjectsInbackgroundWithBlock({(objects:[PFObject]?,error:nserror?)in if error==nil{if let returdObject=objects{//objects array is not nil//遍历数组以获取returdObjects中对象的每个对象{print(object[“

                    }
                })

                let majorSelected:String = (cell?.textLabel!.text!)!
                let query = PFQuery(className:"CollegeMajors")
                query.selectKeys(["Major"])
                query.findObjectsInBackgroundWithBlock ({
                    (objects: [PFObject]?, error: NSError?) in

                    if error == nil {
                        // The find succeeded.
                        print("Successfully retrieved \(objects!.count) majors.", terminator: "")
                        // Do something with the found objects
                        if let returnedobjects = objects {
                            if returnedobjects.contains ({($0["Major"] as? String)! == majorSelected}) && query.selectKeys(["College Name"]) ==  imagesQuery.selectKeys(["name"]) {
                                print("your in!") // transition to the new screen

                            }
                            else {
                                print("your out.") // do whatever
                            }
                        }
                    } else {
                        // Log details of the failure
                        print("Error: \(error!) \(error!.userInfo)", terminator: "")
                    }
                })
            }

        }


    }






}


override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    //Keep track of which major(s) the user selected
    let path = tableView.indexPathForSelectedRow!
    if let cell = tableView.cellForRowAtIndexPath(indexPath){


        //Trigger the segue to go to the collection view
        self.performSegueWithIdentifier("showResults", sender: self)
    }
}

共有1个答案

宓文斌
2023-03-14

你感兴趣的领域有几个问题。

首先,将闭包{[“returdobjects”]作为?String==path}传递给contains(_:)方法,但是闭包不接受任何参数。您需要传递一个带一个参数的闭包,其中它的类型与数组的元素相同。

其次,在闭包中,[“returdobjects”]是一个数组,那么,[“returdobjects”]作为?String总是失败并生成nil。您需要将这一部分更改为产生字符串的有意义的表达式,您可能需要使用传递给这个闭包的PFObject实例。

let path = tableView.indexPathForSelectedRow!

let path=...下面添加一行

let majorSelected: String = (Some expression to retrieve "major" from the `path`)

将包含contains的行中的闭包更改为:

if returnedobjects.contains ({$0["Major"] as? String == majorSelected }) {
 类似资料: