iOS
Querying with NSPredicate
11 min
querying with nspredicate introduction in this section you will learn how to use nspredicate to define your queries in objective c at any time, you can access the complete project built with this tutorial at our github repository prerequisites in this tutorial we will use a basic app created in objective c with xcode 9 1 and ios 11 to complete this tutorial, you need an app created at back4app note follow the new parse app tutorial to learn how to create an app at back4app xcode basic ios app note if you don’t have a basic app created you can open xcode and hit file > new > project > ios then select app after you create your basic app you are ready to follow this guide note parse ios sdk works with ios 7 0 or higher 1 get the template download the template at back4app’s github repository , and unzip files in your project folder you can do that using the following command line $ curl lok https //github com/back4app/ios objective c quickstart example/archive/master zip && unzip master zip 2 open the project template open xcode click on file >open file >open 3\ navigate to the project folder and double click on quickstartobjcexampleapp xcworkspace quickstartobjcexampleapp xcworkspace 4\ wait for xcode to open the project 3 understandig the difference usually for objective c you have two options for building queries using the ‘pfquery’ or the ‘nspredicate’ both work similarly but depending on how many constraints you want to use, it might make more sense using one instead of the other for instance, a simple query using pfquery would be 1 \[query wherekey @"playername" notequalto @"michael yabuti"]; 2 \[query wherekey @"playerage" greaterthan @18]; but a more complex query could become 1 \[query wherekey @"playername" notequalto @"michael yabuti"]; 2 \[query wherekey @"playerage" greaterthan @18]; 3 \[query wherekey @"playerheight" greaterthan @180]; 4 \[query wherekey @"playerweight" greaterthan @80]; 5 \[query wherekey @"playerfavoritecolour" notequalto @"blue"]; 6 \[query wherekey @"playerislefthanded" equalto @true]; 7 \[query wherekey @"playershoesize" notequalto @42]; 8 \[query wherekey @"playerlivingstate" equalto @"arizona"]; 9 \[query wherekey @"playerlivingcity" notequalto @"springfield"]; 10 \[query wherekey @"playermothersname" equalto @"jane"]; so, depending on each case, you can choose to use ‘nspredicate’ instead a simple query using ‘nspredicate’ would be 1 nspredicate predicate = \[nspredicate predicatewithformat @"playername != 'michael yabuti' and playerage > 18"]; 2 pfquery query = \[pfquery querywithclassname @"gamescore" predicate\ predicate]; while a more complex query could become 1 nspredicate predicate = \[nspredicate predicatewithformat @"playername != 'michael yabuti' and playerage > 18 and playerheight > 180 and playerweight > 80 and playerfavoritecolour != 'blue' and playerislefthanded = true and playershoesize != 42 and playerlivingstate = 'arizona' and playerlivingcity != 'springfield' and playermothersname = 'jane'"]; 2 pfquery query = \[pfquery querywithclassname @"gamescore" predicate\ predicate]; 4 executing your query you can, then, execute your query 1 \[query findobjectsinbackgroundwithblock ^(nsarray objects, nserror error) { 2 if (!error) { 3 // request succeeded 4 } 5 }]; next steps at this point, you have learned how to get started with ios apps learn more by walking around our ios tutorials or check parse open source documentation for ios sdk