1

I have a NSMutableDictionary with NSNumbers. When I finish building the set I need to recalculate all the values using the currently stored value itself. Now I'm using fast enumeration and storing into a new NSMutableSet, but I'm not experienced in Objective C and there must be a more efficient way to do this:

for (id key in temp_target_results) 
{
        formula_score = MyFormula([[temp_target_results objectForKey:key] doubleValue]);

        [target_results setObject:[NSNumber numberWithDouble:formula_score] forKey:key];
}

In the end I'm sorting by value (that's why I'm using NSMutableSet).

Miguel E
  • 1,316
  • 2
  • 17
  • 39

1 Answers1

0

I don't know how much more anyone can help you optimize what you are doing because I don't know what's going on behind the scenes (outside of the context of the snippet of code you pasted above, or what's really going on inside your MyFormula function).

One optimization question I have would be: why are you storing everything as NSNumber objects anyways and not an array of doubles? The only advantage (that I can currently see) to doing that is if you're passing your NSNumber objects along in an NSArray, NSSet or NSDictionary that gets written out to disk or passed along in an NSNotification or.

But some of the things I would do would include getting rid of that extra, unnecessary call to objectForKey:

for (NSNumber * myNumber in temp_target_results) 
{
   formula_score = MyFormula([myNumber doubleValue]);

   [target_results setObject:[NSNumber numberWithDouble:formula_score] forKey:[myNumber stringValue]];
}

Another consideration here:

Your target_results appears to not be a NSMutableSet because you are doing a NSMutableDictionary method of setObject:forKey:. If target_results really was a NSMutableSet, you'd only need to call:

[target_results addObject: [NSNumber numberWithDouble: formula_score]];
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215