Friday, July 1, 2016

Objective C NSPredicate Part 4/4

In my earlier posts on NSPredicate, We have discussed,


In this part, let's see some Relational operations along with compound predicates.

  • IN    
  • ALL
  • ANY
  • SOME
  • NONE


We have already covered IN operation in Part 3/4. To discuss remaining Relational operations, let's take another data set.

Let's say fetching all the departments in an organization given the below JSON response.

[

  {

    "name" : "Banking",

    "employees" : [

      {
        "name" : "Tim",
        "salary" : 45000
      },

      {
        "name" : "Smith",
        "salary" : 19000
      },

      {
        "name" : "George",
        "salary" : 30000
      }

    ]

  },


  {

    "name" : "Healthcare",

    "employees" : [

      {
        "name" : "Martin",
        "salary" : 45000
      },

      {
        "name" : "Smith",
        "salary" : 56000
      },

      {
        "name" : "Anderson",
        "salary" : 24000
      }

    ]

  },


  {

    "name" : "Mobility",

    "employees" : [

      {
        "name" : "Martin",
        "salary" : 45000
      },

      {
        "name" : "Smith",
        "salary" : 17000
      },

      {
        "name" : "Anderson",
        "salary" : 24000
      }

    ]

  }


]


This response is in array format and it is having all the departments with their name and employees set.

Let's start our filtration without which we can't live :)





#1 Filter the departments having employee salary less than 20000

Technically, We need all the departments having any of the employee's salary less than 20000.

    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                                                         @"ANY employees.salary < %d",20000];
    
    NSArray *filteredArr = [arrDepartments filteredArrayUsingPredicate:predicate];
    
    NSLog(@"filteredArr : %@",filteredArr);

  filteredArr : (
        {
        employees =         (
                        {
                name = Tim;
                salary = 45000;
            },
                        {
                name = Smith;
                salary = 19000;
            },
                        {
                name = George;
                salary = 30000;
            }
        );
        name = Banking;
    },
        {
        employees =         (
                        {
                name = Martin;
                salary = 32000;
            },
                        {
                name = Smith;
                salary = 17000;
            },
                        {
                name = Anderson;
                salary = 24000;
            }
        );
        name = Mobility;
    }

  )




#2 Filter the departments having all the employees salary greater than 20000



    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                                                          @"ALL employees.salary > %d",20000];
    
    NSArray *filteredArr = [arrDepartments filteredArrayUsingPredicate:predicate];
    
    NSLog(@"filteredArr : %@",filteredArr);


  filteredArr : (
        {
        employees =         (
                        {
                name = Martin;
                salary = 45000;
            },
                        {
                name = Smith;
                salary = 56000;
            },
                        {
                name = Anderson;
                salary = 24000;
            }
        );
        name = Healthcare;
    }

  )

    



#3 Filter the departments having none of the employees salary less then 20000



    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                                                       @"NONE employees.salary < %d",20000];
    
    NSArray *filteredArr = [arrDepartments filteredArrayUsingPredicate:predicate];
    
    NSLog(@"filteredArr : %@",filteredArr);

  filteredArr : (
        {
        employees =         (
                        {
                name = Martin;
                salary = 45000;
            },
                        {
                name = Smith;
                salary = 56000;
            },
                        {
                name = Anderson;
                salary = 24000;
            }
        );
        name = Healthcare;
    }

  )

   


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