Search code examples
iosnsarraynspredicate

NSPredicate for array of dictionary that contains an inner array


I have the following dictionary

(
{
    "_id" = 100;
    status = 0;
    student =     {
        "__v" = 0;
        "_id" = 565b5fb94e8e3b72793407c0;
        age = 9;
        parentDetail =         (
            myson,
            mywifesson
        );
   };
},
{
    "_id" = 101;
    status = 0;
    student =     {
        "__v" = 0;
        "_id" = 565dfc094afc74bb5a006491;
        age = 9;
        parentDetail =         (
            mywifesson
        );
   };
},
{
    "_id" = 102;
    status = 0;
    student =     {
        "__v" = 0;
        "_id" = 565e000f4afc74bb5a006493;
        age = 9;
        parentDetail =         (
            myson
        );

    };
}
) 

I want filter items with the inner parentdetails contains the string "myson". I tried the following predicate but I am getting all the 3 elements in the filtered array. But actually it should contain only first and last element but not the second element

parentid = @"myson"

[NSPredicate predicateWithFormat:@"ANY student.parentDetail CONTAINS[c] %@", parentid];

Solution

  • Instead of CONTAINS try an MATCHES