Question

I got a NSArray that i m fetching from my database and i was using kinda brute force solution where i needed to modify the content of my database for each element. I was using this NSPredicate to filter my array

NSArray *array = [haveIngArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"ANY %@ contains[cd] self", recipeIngredientsArray]];

where recipeIngredientArray are the input came from user and haveIngArray are the fetched content.

this is a sample of the content of my database

1 piece of chicken breast
3 spoon of wheat
6 glass of water
1 can of corn
1 glass of lemon juice
salt

i will compare the inputs with the content came from database but i don't need to compare certain things like salt, water,pepper. How can i delete these from my array so i can use my real filter?

I tried to use NSPredicate with contain and delete the same objects from the original array but that cause performance issues.

Was it helpful?

Solution

Hop This help you ... Just get your DB into MutableArray, then filter it by any words you want

//enter the words you want to filter by
NSArray *filterArray = @[@"water",@"salt"];

NSArray *dataBaseArray = @[@"1 piece of chicken breast", @"3 spoon of wheat", @"6 glass of water", @"1 can of corn", @"1 glass of lemon juice",@"1 piece of chicken breast", @"3 spoon of wheat", @"6 glass of water", @"1 can of corn", @"1 glass of lemon juice"];

// this is the array that get the records from the DB
NSMutableArray *tempArray = [[NSMutableArray alloc]init];
for (int i=0; i <dataBaseArray.count; i++) {

    NSString *str = dataBaseArray[i];
    BOOL found = NO;
    for (NSString *word in filterArray) {

        if ([str rangeOfString:word].location != NSNotFound)
        {
            found = YES;
        }
    }
    if (!found) {
        [tempArray addObject:str];
    }

}

dataBaseArray = [[NSArray alloc]initWithArray:tempArray];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top