ingenspor
ingenspor

Reputation: 932

Code suggestion for searching in NSStrings

I populate my app with a plist array with dictionaries. The value for the key "Freshness" in the dictionaries is the name of a bundle image to show the level of freshness in the drink, "level1.png", "level2.png", "level3.png", "level4.png", "level5.png", "level6.png" or "level7.png".

I have added a UIStepper so the user can specify the minimum and maximum freshness of the drink like this:

- (void)viewDidLoad
{
[super viewDidLoad];
minImageView.image = [UIImage imageNamed:@"level1.png"];
maxImageView.image = [UIImage imageNamed:@"level7.png"];
}

- (IBAction)minStepperValueChanged:(id)sender {
double stepperValue = minStepper.value;
minImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"level%.f.png", stepperValue]];
maxStepper.minimumValue = minStepper.value;
minStepper.maximumValue = maxStepper.value;
minLabel.text = [NSString stringWithFormat:@"From level %.f", stepperValue];
}

- (IBAction)maxStepperValueChanged:(id)sender {
double stepperValue = maxStepper.value;
maxImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"level%.f.png", stepperValue]];
maxStepper.minimumValue = minStepper.value;
minStepper.maximumValue = maxStepper.value;
maxLabel.text = [NSString stringWithFormat:@"To level %.f", stepperValue];
}

When searchButtonPressed is called, guess what I want to happen. To add the objects with the correct level of freshness from allObjectsArray to searchResultsArray. Any suggestion for code to filter like this?

I was thinking something like using a mutable array levelsArray with all the level image paths, then remove the objects that goes below minimum or above maximum from levelsArray when searchButtonPressed.

Then add the dictionaries that match one of the remaining objects of this array to searchResultsArray. But I've twisted my brain so much on this that I really need some help. Thanks a lot!

EDIT:

I've done this for a start:

-(IBAction)searchButtonPressed:(id)sender{

for(double i=otherCriteriasViewController.minStepper.value;i>1;i--) {
    [otherCriteriasViewController.levelsArray removeObject:[NSString stringWithFormat:@"level%.f.png", i-1]];
}

for(double ii=otherCriteriasViewController.maxStepper.value;ii<7;ii++) {
    [otherCriteriasViewController.levelsArray removeObject:[NSString stringWithFormat:@"level%.f.png", ii+1]];
}

and now I want to add all the objects that contain one of the objects of levelsArray.

Upvotes: 1

Views: 78

Answers (1)

Dima
Dima

Reputation: 23624

If I am understanding correctly, the problem you are trying to solve is filtering an array of dictionaries based on a key that is in each of the dictionaries.

You can use an NSPredicate to do this.

Assuming you have an NSArray called "allItems" and that each object in the array is a dictionary containing the key "Freshness".

Let's say you want all the items with a Freshness of 2.

NSArray *filtered = [allItems filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(Freshness == %@)", @"level2.png"]];

You can also do greater than and less than comparisons using NSPredicates. This is just an equals example.

Upvotes: 1

Related Questions