使用parse过滤查询 - swift2

时间:2015-11-08 19:50:22

标签: parse-platform swift2.1

我试图在解析中使用多个条件进行查询,例如,我只输入了一个职业并将所有其他字段留空,然后点击搜索按钮。它将查询所有占用数据,但之后是与过滤器无关的其他数据。

如何根据用户过滤搜索限制结果。

问题是我的查询会返回解析中的所有数据,

    let occupationQuery = PFQuery(className: "JobPost")
    occupationQuery.whereKey("occupation", containsString: occupationLabel.text)

    let genderQuery = PFQuery(className: "JobPost")
    genderQuery.whereKey("gender", containsString: genderLabel.text)

    let workExperienceQuery = PFQuery(className: "JobPost")
    workExperienceQuery.whereKey("workExperience", containsString: workExperienceLabel.text)

    let employmentStatusQuery = PFQuery(className: "JobPost")
    employmentStatusQuery.whereKey("employerStatus", containsString: employmentLabel.text)

    let nationalityQuery = PFQuery(className: "JobPost")
    nationalityQuery.whereKey("nationality", containsString: nationalityLabel.text)

    let finalQuery = PFQuery.orQueryWithSubqueries([occupationQuery, genderQuery, workExperienceQuery, employmentStatusQuery, nationalityQuery])

    finalQuery.findObjectsInBackgroundWithBlock { (results: [AnyObject]?, error) -> 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)
                print(objects)
            print(objects)

        }
    }

1 个答案:

答案 0 :(得分:0)

似乎我使用了错误的函数,将containsString更改为equalTo。

employmentStatusQuery.whereKey("employerStatus", equalTo:
employmentLabel.text!)