Swift:无法转换类型'() - >的值布尔'期望参数类型' PFObject'

时间:2016-07-02 15:50:56

标签: ios swift parsing

在整体方案中,我试图比较用户从tableview中选择的多个选项,并将它们与我的Parse数据库进行比较。所以我的问题是双重的1.我现在的代码是正确的吗? 2.如何将值类型Bool转换为参数类型PFObject?

  

无法转换类型'() - >的值布尔'预期参数类型' PFObject'

enter image description here

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:&#34; CollegeImages&#34;)                     imagesQuery.selectKeys([&#34;名称&#34;])                     imagesQuery.findObjectsInBackgroundWithBlock({(objects:[PFObject] ?, error:NSError?)in                         如果错误== nil {                             如果让returnobjects = objects {                                 //对象数组不是没有                                 //循环遍历数组以获取每个对象                                 returnobjects中的对象{                                     print(object [&#34; name&#34;] 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)
    }
}

1 个答案:

答案 0 :(得分:0)

您的兴趣点有几个问题。

首先,您将一个闭包{["returnedobjects"] as? String == path }传递给contains(_:)方法,但闭包不会带任何参数。你需要传递一个带有一个参数的闭包,其中的类型与数组的元素相同。

其次,在闭包内,["returnedobjects"]是一个数组,因此,["returnedobjects"] as? String总是失败并生成nil。您需要将此部分更改为生成String的有意义的表达式,您可能需要使用传递给此闭包的PFObject个实例。

第三,您将path声明为:

let path = tableView.indexPathForSelectedRow!

表示局部变量的类型为NSIndexPath。因此,即使==的左侧返回有效的String,您也无法将StringNSIndexPath进行比较。在比较之前,您可能需要获得String值。

考虑到上述所有三个并且有一些猜测,你需要:

let path = ...

下添加一行
let majorSelected: String = (Some expression to retrieve "major" from the `path`)

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

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