簡體   English   中英

Swift:無法將類型'() - > Bool'的值轉換為預期的參數類型'PFObject'

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

在整體方案中,我試圖比較用戶從tableview中的多個選擇,並將它們與我的Parse數據庫進行比較。 所以我的問題是雙重的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 if error == nil {if let returnedobjects = objects {// objects數組不是nil //循環遍歷數組以獲取返回對象中的對象的每個對象{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)
    }
}

您的興趣點有幾個問題。

首先,你傳遞一個閉包{["returnedobjects"] as? String == path } {["returnedobjects"] as? String == path } to contains(_:)方法,但是閉包不帶任何參數。 你需要傳遞一個帶有一個參數的閉包,其類型與數組的元素相同。

第二,在閉包內部, ["returnedobjects"]是一個數組,所以, ["returnedobjects"] as? String ["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 }) {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM