Search code examples
iosobjective-ciphonensmutablearraynsarray

How to create NSMutableArray using NSPredicate from the NSMutableArray/NSMutableDictionary


I have NSMutable array which contains certain fields in its index as a key and value pair like this

Result =     (
            {
        FName = @"Achal";
        MName = @"Pradip";
        LName = @"Gandhi";
        AGE =  35;
    },
    {
        FName = @"Jenish";
        MName = @"Pavan";
        LName = @"Patel";
        AGE = 40;
    }
    {
        FName = @"Pritesh";
        MName = @"Amit";
        LName = @"Dave";
        AGE = 50;
    }
    {
        FName = @"Mehul";
        MName = @"Nikhil";
        LName = @"Shah";
        AGE = 40;
    }
);

From that array i want to filter it and i need out put Using NSPredicate like this

FilterArray = {
        FName = @"Achal";
        LName = @"Gandhi";
    },
    {
        FName = @"Jenish";
        LName = @"Patel";
    }
    {
        FName = @"Pritesh";
        LName = @"Dave";
    }
    {
        FName = @"Mehul";
        LName = @"Shah";
    }
);

Only "FName" & "LName" keys are Required from the original array.


Solution

  • I made solution with this below code and its perfectly work.

    NSMutableArray *tempArray = [[NSMutableArray alloc]init];
    
    
    for(int i=0;i < self.Result.count;i++){
        NSMutableDictionary *cartData = [[NSMutableDictionary alloc]init];
    
        [cartData setObject:[[Result objectAtIndex:i] valueForKey:@"Fname"] forKey:@"FName"];
    
    
        [cartData setObject:[[Result objectAtIndex:i] valueForKey:@"LName"] forKey:@"LName"];
    
        [tempArray addObject:cartData];
    }
    

    But this is done by a loop but i want to use and solve it with the help of NSPredicate .