简体   繁体   中英

Swift: Cannot convert value of type '() -> Bool' to expected argument type 'PFObject'

In the overall scheme of things I am trying to compare the user's multiple selections from a tableview and compare them to my Parse database. So my problem is twofold 1. Is my current code going about it the correct way? and 2. How can I convert value type Bool to argument type PFObject?

Cannot convert value of type '() -> Bool' to expected argument type '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!)

                }

let imagesQuery = PFQuery(className:"CollegeImages") imagesQuery.selectKeys(["name"]) imagesQuery.findObjectsInBackgroundWithBlock({(objects: [PFObject]?, error: NSError?) in if error == nil { if let returnedobjects = objects { //objects array isn't nil //loop through the array to get each object for object in returnedobjects { print(object["name"] as! String) } }

                    }
                })

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

Your line of interest holds several problems.

First of all, you are passing a closure {["returnedobjects"] as? String == path } {["returnedobjects"] as? String == path } to contains(_:) method, but the closure does not take any arguments. You need to pass a closure taking one argument, where its type being the same as the element of the array.

Second, inside the closure, ["returnedobjects"] is an array, so, ["returnedobjects"] as? String ["returnedobjects"] as? String always fails and generates nil . You need to change this part to a meaningful expression producing String , you may need to utilise the PFObject instances passed to this closure.

Third, you declare path as:

let path = tableView.indexPathForSelectedRow!

which means the local variable has type NSIndexPath . So, even if the left hand side of == returns a valid String , you cannot compare String to NSIndexPath . You may need to get a String value before comparing.


With considering all three above and with some guess, you need to:

Add one line below let path = ...

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

Change the closure in the line containing contains as:

if returnedobjects.contains ({$0["Major"] as? String == majorSelected }) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM