Mohammed Hussain Mohammed Hussain - 3 months ago 21
iOS Question

Filter NSMutableArray with All the values of nsarray

I have MutableArray like this...

restaurants = ({
cuisines = ({
"cuisine_id" = 1;
"cuisine_name" = Indian;
},
{
"cuisine_id" = 2;
"cuisine_name" = SriLanka;
}
);}

{ cuisines = ({
"cuisine_id" = 1;
"cuisine_name" = Chinese;
},
{
"cuisine_id" = 2;
"cuisine_name" = Afghani;
}
);}

{cuisines =({
"cuisine_id" = 1;
"cuisine_name" = Chinese;
},
{
"cuisine_id" = 2;
"cuisine_name" = Afghani;
}
);}
)


And I'm Filtering Restaurants Array With multiple values of cuisine_name
For Example if I have a,

Nsarray like this cuisine_name (
Indian,
Srilankan,
Chinese,
)

I want to filter my Restaurants array on basis of each object of cusine_name.

At present I'm doing like this..

NSPredicate *predicateCuisines = [NSPredicate predicateWithFormat:@"(cuisines.cuisine_name = %@)",[cuisineTypes valueForKey:@"cuisine_name"]];
[self.arrRestaurantList filterUsingPredicate:predicateCuisines];


But it filtering Using description...For Restaurant Array which contains whole three values(Indian,Srilankan,Chinese) but i want to filter with each object of cuisine_name.
Any help will really appreciated..

Answer

plz use this

  NSArray* arrtu4 = @[@{@"cuisines" : @[@{@"cuisine_id" : @1,@"cuisine_name" : @"Indian"},@{ @"cuisine_id" : @2, @"cuisine_name" : @"SriLanka"}]},@{ @"cuisines" : @[@{@"cuisine_id" : @1,@"cuisine_name" : @"Chinese"},@{@"cuisine_id" : @2,@"cuisine_name" : @"Afghani"}]},@{@"cuisines" :@[@{@"cuisine_id" :@1,@"cuisine_name" : @"Chinese"},@{ @"cuisine_id" : @2,@"cuisine_name" : @"Afghani"}]}];
    NSMutableArray *arrryfilted = [NSMutableArray array];

    NSArray *arrayCountrys = @[@"Chinese",@"SriLanka",@"Indian"];

for (int i = 0; i<arrtu4.count; i++)
{

    NSArray *filterArray2 = [[arrtu4 objectAtIndex:i]valueForKey:@"cuisines"];

    NSArray *filterArray3 = [filterArray2 filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id  _Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
        return [arrayCountrys containsObject:[evaluatedObject valueForKey:@"cuisine_name"]]  ;

                }]];




     [arrryfilted addObjectsFromArray:filterArray3 ];



}

NSLog(@"%@",arrryfilted);

output:

2016-08-20 16:44:59.856 coredataReltaion[6762:152672] (
        {
        "cuisine_id" = 1;
        "cuisine_name" = Indian;
    },
        {
        "cuisine_id" = 2;
        "cuisine_name" = SriLanka;
    },
        {
        "cuisine_id" = 1;
        "cuisine_name" = Chinese;
    },
        {
        "cuisine_id" = 1;
        "cuisine_name" = Chinese;
    }
)