简体   繁体   中英

OR Queries in Parse for iOS

I am trying to create a compound query for my app to search the list of PFUsers in my Parse database whose firstname or lastname or username contain a specific word entered using the keyboard.

To do that, my code is:

let usernameQuery = PFUser.query()
usernameQuery!.whereKey("username", containsString: searchBar.text?.lowercaseString)

let firstnameQuery = PFUser.query()
usernameQuery!.whereKey("firstname", containsString: searchBar.text?.lowercaseString)

let lastnameQuery = PFUser.query()
usernameQuery!.whereKey("Lastname", containsString: searchBar.text?.lowercaseString)

let query = PFQuery.orQueryWithSubqueries([usernameQuery, firstnameQuery, lastnameQuery])
query.findObjectsInBackgroundWithBlock{ ......

My problem is that i have an error creating the Array of Subqueries with the following error message: Cannot convert value of type '[PFQuery?]' to expected argument type '[AnyObject]' I'm not that familiar yet with Swift and was wondering if anyone had any clue on how to solve that by any chance?

Thanks in advance for your help!

There's a couple issues going on here.

In terms of syntax, notice the ! in usernameQuery!.whereKey . This is telling us that usernameQuery is an optional value which needs to be unwrapped.

So the correct compound query should be

let query = PFQuery.orQueryWithSubqueries([usernameQuery!, firstnameQuery!, lastnameQuery!])

Next, you will want to call whereKey on each separate query, rather than all of them on usernameQuery . I'm guessing those are classic copy/paste typos :)

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