iphonemaclover iphonemaclover - 4 months ago 16
Objective-C Question

NSPredicate to get Array containing specific key value

Get all elements of array using item-"apple"

My input

item_list:
[{
CurrentDate = “4/2/2016";
search_item =(

item_name = @“apple”;
);
},

{
CurrentDate = “27/1/2016";
search_item =(
item_name = @“Ball”; );
},
{
CurrentDate = “17/2/2016";
search_item =(
item_name = @“Cat”; );
},
{
CurrentDate = “6/2/2016";
search_item =(
item_name = @“apple”; );
},

]


Output i need

item_list:
[{
CurrentDate = “4/2/2016";
search_item =(

item_name = @“apple”;
);
},

,
{
CurrentDate = “6/2/2016";
search_item =(
item_name = @“apple”; );
},

]


Code i try but not working

NSArray* filtered = [[item_list valueForKey:@"search_item"] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(item_name == %@)", @“apple”]];

Answer
NSPredicate *predicate =
[NSPredicate predicateWithFormat:@"ANY search_item.item_name CONTAINS[c] %@", @"apple"];  
NSArray *array =[item_list filteredArrayUsingPredicate:predicate];
Comments