Sunday, June 26, 2016

Objective C NSPredicate Part 1/4

NSPredicate is very useful for fetching and filtering. This can be best explained using the practical examples instead of a big theory. Let's take some real time use cases where using NSPredicate is going to be only choice.

I have an array of strings. I can check whether a string "Mango" is there in the array or not using the containsObject(). But what If I need to filter all the objects which contains the word 'an'. For this, we need to loop through array and check each string whether it contains our word or not.



    NSArray *arr = @[@"Apple",@"Mango",@"Banana",@"Papaya"];

    NSMutableArray *filteredArr = [[NSMutableArray allocinit];
    
    for(int i=0;i<count;i++){
        
        NSString *fruit = [arr objectAtIndex:i];
        
        if([fruit containsString:@"an"]){
            
            [filteredArr addObject:fruit];
        }
        
    }

  NSLog(@"filteredArr-Loop : %@",filteredArr);

    filteredArr-Loop : (

       Mango,
       Banana


   ) 



Instead of looping an array having huge number of items, We can evaluate an object using the predicates. Let's see how can we achieve the same result using the NSPredicate.  


   
     NSArray *arr = @[@"Apple",@"Mango",@"Banana",@"Papaya"];
   
     NSArray *filteredArr = [arr filteredArrayUsingPredicate:    
                  [NSPredicate predicateWithFormat:@"SELF contains %@",@"an"]];
    
      NSLog(@"filteredArr-Predicate : %@",filteredArr);


    filteredArr-Predicate : (

           Mango,
           Banana

    )



Same result with just a simple query.



   SELF contains %@


In this query,



  SELF - each element of the array
  contains - checking If the element contains our word

  %@ - placeholder for our query string



Now we got an idea of using the predicates and the usefulness of those. 



                      




Let's see some Basic comparisons using NSPredicate.


   
    NSArray *numbersArr = @[@10000,@2000,@5000,@3500];    
    
    NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF <= %d",
                    [[NSNumber numberWithInt:5000intValue]];
    
    NSArray *filteredArr = [numbersArr filteredArrayUsingPredicate:predicate];
    
    NSLog(@"filteredArr : %@",filteredArr);

       filteredArr : (

            2000,
           5000,
           3500

       )



In the above example, We are filtering all the numbers,

Which are less than or equal to 5000 from the numbers array.



   
SELF <= %d",[[NSNumber numberWithInt:5000intValue]



For the numbers, Which are greater than 5000.



   
SELF > %d",[[NSNumber numberWithInt:5000intValue]



For the numbers, Which are not equal to 5000.



   
SELF != %d",[[NSNumber numberWithInt:5000intValue]



For the numbers, Which are equal to 5000.



   
SELF = %d",[[NSNumber numberWithInt:5000intValue]



What If we need a number which is in between 5000 and 8000. We have BETWEEN to fetch these.




   
 NSArray *numbersArr = @[@10000,@2000,@5000,@3500];
    
    
    NSPredicate* predicate = [NSPredicate predicateWithFormat:
                 @"SELF BETWEEN {%d, %d}",[[NSNumber numberWithInt:3000],          
                                          [[NSNumber numberWithInt:8000intValue]];
    
    NSArray *filteredArr = [numbersArr filteredArrayUsingPredicate:predicate];
    
    NSLog(@"filteredArr : %@",filteredArr);

    filteredArr : (

           5000,
           3500 
     )



That's how using the NSPredicate, We can query lists and get required results instead of writing loops.

In the next part, Let's discuss some more comparisons and use cases of NSPredicate.


Hope this post is useful. Feel free to comment in case of any queries.