Vix Hunk Vix Hunk - 2 months ago 7
Objective-C Question

Filtering NSMutableArray and saving it back in itself

I have my

data loaded from a
but I want to filter is as well. data is loading perfectly but now I need to apply filtering function on it and reload table. here is my code so far that I wrote for filtering but its not working

NSPredicate *sPredicate;
for (int i=0; i<TableArray.count; i++) {
float hotelDistanceFloat = [[[TableArray objectAtIndex:i]xmlhotel_distance] floatValue];
NSInteger hotelPrice = [[[TableArray objectAtIndex:i]xmlhotel_price] integerValue];

sPredicate = [NSPredicate predicateWithFormat:@"(%ld <= %d AND %f <= %d)" , (long)hotelPrice, numberOfBudget, hotelDistanceFloat,numberOfDistance];
TableArray = [[TableArray filteredArrayUsingPredicate:sPredicate] mutableCopy];


are simple int values getting from uislider.
is my mutablearray having all the tabledata in it.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return TableArray.count;

these are the values that my nsmutablearray contains

NSLog(@"Name : %@",[[TableArray objectAtIndex:1]xmlhotel_name]);
NSLog(@"City : %@",[[TableArray objectAtIndex:1]xmlhotel_city]);
NSLog(@"Price : %@",[TableArray objectAtIndex:1]xmlhotel_price);
NSLog(@"Distance : %@",[TableArray objectAtIndex:1]xmlhotel_distance);
NSLog(@"Image : %@",[[TableArray objectAtIndex:1]xmlhotel_image]);
NSLog(@"Stars : %@",[[TableArray objectAtIndex:1]xmlhotel_stars]);

all these values are STRINGS


You probably can remove the for loop, you don't need to assign array and create predicate each and every time.

   NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"(xmlhotel_price.intValue <= %d AND xmlhotel_distance.floatValue <= %f)", numberOfBudget, numberOfDistance];
   NSMutableArray *filteredArray = [NSMutableArray arrayWithArray:[TableArray filteredArrayUsingPredicate:sPredicate]];