So I've stored an NSDictionary object in an NSMutableArray, how do I know use the NSDictionary keys to grab these objects out of the array?

StackOverflow https://stackoverflow.com/questions/23553976

Question

I have two UISlider's that represent minimum and maximum prices of items. I am passing these as well as other various data back to the previous controller.

I've used a protocol method and set the previous controller as a delegate to make it possible to pass values back to the controller.

I can easily grab the other objects out of the array because they're strings. I just use:

     [_finalSelectionForRefinement containsObject:@"size"];

This is what I do in pushed controller:

      // create dictionary with keys minimum and maximum that hold the 
      // position of the slider as a float  

      _dictionaryWithSliderValues = 
      [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithFloat:_minSliderPosition], @"minimum",
      [NSNumber numberWithFloat:_maxSliderPosition], @"maximum", nil];

      // store this in the array that is retrieved in previous controller
      [_finalSelectionForRefinement addObject:_dictionaryWithSliderValues];

My question is how do I now use the minimum and maximum keys to grab the slider position float objects?

Thought I could use NSPredicate but the examples I've been coming across on blogs as well as youTube are of no help to my specific needs.

Would appreciate some help here

Regards

UPDATE - Short snipped of method in previous controller where I need to retrieve the slider minimum and maximum values:

-(PFQuery *)queryForCollection
{
    PFQuery *query = [PFQuery queryWithClassName:@"Garments"];

        if (_selectedRowInFilterPicker == 0) {
            NSLog(@"ORDER BY RECOMMENDED");
            [query orderByDescending:@"recommended"];
        } else if (_selectedRowInFilterPicker == 1) {
            NSLog(@"ORDER BY NEWEST ITEMS");
            [query orderByDescending:@"createdAt"];
        } else if (_selectedRowInFilterPicker == 2) {
            NSLog(@"ORDER BY DESCENDING USING PRICE");
            [query orderByDescending:@"price"];
        } else if (_selectedRowInFilterPicker == 3) {
            NSLog(@"ORDER BY ASCENDING USING PRICE");
            [query orderByAscending:@"price"];
        }


        if ([_selectionFromRefineResultsController count] > 0) {
            NSLog(@"Selection FROM REF MADE");

            // Gender
            if ([_selectionFromRefineResultsController containsObject:@"Male"]) {
                [query whereKey:@"gender" equalTo:@1];
            }
            if ([_selectionFromRefineResultsController containsObject:@"Female"]) {
                [query whereKey:@"gender" equalTo:@2];
            }
        }

        // Here I need to check there is a minimum or maximum value in the array
        // If there is I can user [query whereKey:@"price" greaterThan:MINVAL] and MAXVAL
        // This will return items within the correct price range.

This queryForCollection method is called from within another method called performQuery which is called when the button of the second controller is tapped to pass data back to the controller that pushed it in the first place.

Was it helpful?

Solution

You should look at the documentation for NSMutableArray , NSArray and NSDictionary

Which will explain the instance methods for each.

But in a nutshell any object that you add should be in a NSDictionary so it has a value and a key. This includes any of your strings. Doing so simplifies how you search by using keys.

If the NSMutableArray contains objects that are not KVC then you will I think find it harder it go through the objects in one sweep.

Because NSmutableArray inherites from NSArray you can then use instance method valueForKey: on a NSmutableArray whose objects values or objects objects values have keys.

valueForKey:

Returns an array containing the results of invoking valueForKey: using key on each of the array's objects.

  • (id)valueForKey:(NSString *)key

Rough Example:

  NSMutableArray * finalSelectionForRefinement =[[NSMutableArray alloc]init];
  NSDictionary *dictionaryWithSliderValues = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithFloat:10], @"minimum", [NSNumber numberWithFloat:20], @"maximum", nil];
    
     NSDictionary *stringValues = [[NSDictionary alloc] initWithObjectsAndKeys:@"the-size", @"size", @"the-hight", @"hight", nil];
    
  
    [finalSelectionForRefinement addObject:dictionaryWithSliderValues];
     [finalSelectionForRefinement addObject:stringValues];
    
    
    
    
    NSLog(@"finalSelectionForRefinement = %@", [finalSelectionForRefinement valueForKey:@"maximum"] );

First off, you can of store everything in one NSDictionary which makes more sense. But I wanted to show you that the valueForKey: will search within each.

The other thing is valueForKey: will return an NSArray containing the results. any objects that it finds that do not match the key you are looking for will be returned as an NSNull object. i.e

finalSelectionForRefinement = (
    20,
"<null>"
)

So you would need to still single your value out. One way is use a objectEnumerator like this:

 NSEnumerator *enumerator = [[finalSelectionForRefinement valueForKey:@"maximum"] objectEnumerator];
    id anObject;
    
    while (anObject = [enumerator nextObject]) {
       
        if(![anObject  isKindOfClass:[NSNull class]])
         {
          NSLog(@"anObject = %@", anObject);
        }
       
    }

Which should return:

 anObject = 20

There are most likely better ways of doing this. All of the above is just to give you one idea. And I suspect you could cut out a lot of the code by using bindings.

(also note this answer was being constructed before you question update)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top