У меня есть серия из 5 iVars. (highscore01, highscore02, highScore03, highScore04, highScore05) Я хочу обновить конкретный iVar с целочисленным значением. IVars определяются как целые. IVars относятся к классу HighScores.
Конкретный обновляемый iVar - это тот, в котором хранится наименьшее значение тока. Я хочу заменить самое низкое значение новым значением.
У меня есть метод, который идентифицирует ivar с наименьшим значением и возвращает строку «theString», содержащую имя обновляемого iVar.
Мой вопрос: как использовать «theString» для обновления правильного iVar.
Вот пример кода.
// 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
}
В последней строке я пытаюсь установить iVar, указанный в "theString", на новый номер счета. «theString» содержит имя обновляемого iVar, то есть «HighScore03» и т. д.
Если бы я настраивал это вручную, это было бы;
theHighScores.highScore03 = mainScores.scoreTotal;
Любое понимание будет высоко ценится.