Search code examples
iosswiftparse-platformswift2pfquery

Filter users based on their specified age ranges. Parse and Swift


Column in Parse called Age with the users age saved as a number. I have two numbers saved as minAge and maxAge. ie. minAge = 15 maxAge = 20 I only want to query users with an age between this range.

I want to query users who age falls between the age range specified.

How do I make this query? or does anyone have any examples they could show me.

query?.whereKey("age", lessThanOrEqualTo: maxAge, greaterThanOrEqualTo: minAge)

Solution

  • You can give multiple constraints, and objects will only be in the results if they match all of the constraints. In other words, it's like an AND of constraints.

    Since you want to query for users where (age <= maxAge) && (age >= minAge), this corresponds to applying two constraints to the query:

    query?.whereKey("age", lessThanOrEqualTo: maxAge)
    query?.whereKey("age", greaterThanOrEqualTo: minAge)
    

    Here's more information on query constraints.