views:

41

answers:

3

I have a series of 5 iVars. (highscore01, highscore02, highScore03, highScore04, highScore05) I want to update a particular iVar with an integer value. The iVars are defined as ints. The iVars are in a Class called HighScores.

The particular iVar to be updated is the one with the lowest current value stored in it. I want to replace the lowest value with a new value.

I have a method that identifies the ivar with the lowest value and returns a String "theString" containing the name of the iVar to be updated.

My question: How do I use the "theString" to update the correct iVar.

Here is a sample of the code.

// If any of the highScore iVars contain 0, then they are still empty.
// Find the first empty iVar and store score there.

 if (scoreVarsFullFlag == NO) // Flag to indicate if any iVars are still zero
 {
 if (theHighScores.highScore01 == 0)
  theHighScores.highScore01 = mainScores.scoreTotal;
 else if (theHighScores.highScore02 == 0)  
  theHighScores.highScore02 = mainScores.scoreTotal;
 else if (theHighScores.highScore03 == 0)
  theHighScores.highScore03 = mainScores.scoreTotal;
 else if (theHighScores.highScore04 == 0)
  theHighScores.highScore04 = mainScores.scoreTotal;
 else if (theHighScores.highScore05 == 0)
  {
   theHighScores.highScore05 = mainScores.scoreTotal;
   scoreVarsFullFlag = YES; // Last scores iVar turns nonzero - set Flag to YES, to indicate no non-zero iVars
  }
 }
 else
 {

  NSLog(@"The Lowest is at %@", [theHighScores findLowestHighScore]);
  NSString * theString;
  theString = [NSString stringWithString:[theHighScores findLowestHighScore]];
  NSLog(@"The String is: %@", theString);
            theHighScores.theString = mainScores.scoreTotal; // This fails
}

The last line is where I try to set the iVar identified in "theString" to the new score number. "theString" does contain the name of the iVar to update, i.e. "HighScore03", etc.

If I was setting this manually, it would be; theHighScores.highScore03 = mainScores.scoreTotal;

Any insight would be much appreciated.

+1  A: 

If I were you I'd just use a mutable array instead to allow sorting and easily pick off the lowest item.

///store this in your app delegate
NSMutableArray *highscores = [[NSMutableArray alloc] init];



//then when you want to add a high score
[highscores addObject:[NSNumber numberWithDouble:mainScores.scoreTotal]];

NSSortDescriptor *myDescriptor;
myDescriptor = [[NSSortDescriptor alloc] initWithKey:@"doubleValue" ascending:NO];
[highscores sortUsingDescriptors:[NSArray arrayWithObject:myDescriptor]];

///remove the last object if it's over 5
if ([highscores count]>5) {
 [highscores removeLastObject];
}
mjdth
Thanks for the help. Setting up a mutable array was definitely the better approach.
ReachWest
+1  A: 

I think mjdth's solution is probably the best, but you could also use -setValue:forKey:, though you'd have to switch to using NSNumbers, rather than ints.

[theHighScores setValue: [NSNumber numberWithInt: mainScores.scoreTotal] forKey: [theHighScores findLowestHighScore]];
Ben Gottlieb
A: 

Sounds like you're trying to do some basic type introspection. Specifically, using NSSelectorFromString.

int newValue = 1;
SEL methodName = NSSelectorFromString(@"setHighScore03:");
[theHighScores performSelector:methodName withObject:newValue];
slf