Reputation: 195
i used the NsPredicate to Filter the NSMutableArray By one & multiple Values,First i tried by to filter By Price, i have saved the NSDictionary Value to the NSMutableArray (i.e) resultArray,here is my code help me,
for (int i=0; i<=resultArray.count; i++)
{
NSDictionary *dict=resultArray[i];
NSLog(@"Dict %@",dict);
NSPredicate *predit=[NSPredicate predicateWithFormat:@"(Price == %@)", @"100"];
NSArray *resultAr = [[dict allValues] filteredArrayUsingPredicate:predit];
NSLog(@"Output %@",resultAr);
}
Dict {
Name = "Black Eyed Peas";
Percentage = 0;
Price = 80;
}
And Result Array is:
Result (
{
Name = "Black Eyed Peas";
Percentage = 0;
Price = 80;
},
{
Name = "Black Gram";
Percentage = 0;
Price = 56;
},
{
Name = "Channa White";
Percentage = 0;
Price = 100;
},
{
Name = "Double Beans";
Percentage = 0;
Price = 95;
},
{
Name = "Gram Dall";
Percentage = 0;
Price = 100;
},
{
Name = "Green Moong Dal";
Percentage = 0;
Price = 150;
},
{
Name = "Ground Nut";
Percentage = 0;
Price = 140;
},
{
Name = "Moong Dal";
Percentage = 0;
Price = 75;
},
{
Name = "Orid Dal";
Percentage = 0;
Price = 100;
},
{
Name = "Toor Dal";
Percentage = 0;
Price = 150;
}
)
Expected Output is
(
{
Name = "Channa White";
Percentage = 0;
Price = 100;
},
{
Name = "Gram Dall";
Percentage = 0;
Price = 100;
},
{
Name = "Orid Dal";
Percentage = 0;
Price = 100;
}
)
but it giving the Error
reason: '[<__NSCFNumber 0x146db5e0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key Price.
above code is correct for filtering else give me a idea to filter the resultArray for expected output
Upvotes: 0
Views: 257
Reputation: 9609
Follow the below code.I use predicate here.
NSPredicate *predit=[NSPredicate predicateWithFormat:@"Price like %@",100];
NSArray *resultAr = [resultArray filteredArrayUsingPredicate:predit];
In above code
If you give LIKE it gives same price data.
You have to Delete or remove [dict allValues] from the code.
Instead of [dict allValues] you need to add resultArray.Because it is array.
Upvotes: 1
Reputation: 2380
You Have Array like this
NSArray *arr = @[@{
@"Name": @"Black Eyed Peas",
@"Percentage" : @"0",
@"Price" : @"80"
},
@{
@"Name" : @"Black Gram",
@"Percentage" : @"0",
@"Price" : @"56"
},
@{
@"Name" : @"Channa White",
@"Percentage" : @"0",
@"Price" : @"100"
},
@{
@"Name" : @"Double Beans",
@"Percentage" : @"0",
@"Price" : @"95"
},
@{
@"Name" : @"Gram Dall",
@"Percentage" : @"0",
@"Price" : @"100"
},
@{
@"Name" : @"Green Moong Dal",
@"Percentage" : @"0",
@"Price" : @"150"
},
@{
@"Name" : @"Ground Nut",
@"Percentage" : @"0",
@"Price" : @"140"
},
@{
@"Name" : @"Moong Dal",
@"Percentage" : @"0",
@"Price" : @"75"
},
@{
@"Name" : @"Orid Dal",
@"Percentage" : @"0",
@"Price" : @"100"
},
@{
@"Name" : @"Toor Dal",
@"Percentage" : @"0",
@"Price" : @"150"
}];
You just have to use predicate on this array like
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Price like %@",@"100"];
NSArray *arr2 = [arr filteredArrayUsingPredicate:predicate];
and you're getting all values from array which have price 100
Upvotes: 0