如何使以下代码干燥?

1 cocoa dry objective-c

如何使我的下列代码"干"(不要重复自己)

- (void)updateLetterScore { // NOT DRY... Must fix
    if (percentScore < 60.0)
        letterLabel.text = [NSString stringWithFormat:@"F"];

    if (percentScore > 59.0 && percentScore < 64.0)
        letterLabel.text = [NSString stringWithFormat:@"D-"];

    if (percentScore > 64.0 &&  percentScore < 67.0)
        letterLabel.text = [NSString stringWithFormat:@"D"]; 

    if (percentScore > 66.0 &&  percentScore < 70.0)
        letterLabel.text = [NSString stringWithFormat:@"D+"]; 

    if (percentScore > 69.0 &&  percentScore < 74.0)
        letterLabel.text = [NSString stringWithFormat:@"C-"]; 

    if (percentScore > 73.0 &&  percentScore < 76.0)
        letterLabel.text = [NSString stringWithFormat:@"C"];

    if (percentScore > 76.0 &&  percentScore < 80.0)
        letterLabel.text = [NSString stringWithFormat:@"C+"];

    if (percentScore > 79.0 &&  percentScore < 84.0)
        letterLabel.text = [NSString stringWithFormat:@"B-"];

    if (percentScore > 83.0 &&  percentScore < 86.0)
        letterLabel.text = [NSString stringWithFormat:@"B"];

    if (percentScore > 85.0 &&  percentScore < 90.0)
        letterLabel.text = [NSString stringWithFormat:@"B+"];

    if (percentScore > 89.0 &&  percentScore < 94.0)
        letterLabel.text = [NSString stringWithFormat:@"A-"];

    if (percentScore > 93.0 &&  percentScore < 100.0)
        letterLabel.text = [NSString stringWithFormat:@"A"];

    if (percentScore == 100)
        letterLabel.text = [NSString stringWithFormat:@"A+"];
}
Run Code Online (Sandbox Code Playgroud)

谢谢你的提示.我只是想知道你们的想法,因为这个小小的snippit在我的代码中看起来很糟糕.

Tal*_*joe 11

一种方式(伪代码,因为我不知道目标C):

grades = ["F", "D-", "D", ...]
scores = [60.0, 64.0, 67.0, ...]

for(i = 0; i < grades.count; i = i + 1)
{
   if(score < scores[i])
   {
     letterLabel.text = [NSString stringWithFormat:@"%@", grades[i]]
     break;
   }
}
Run Code Online (Sandbox Code Playgroud)