0

I have a UITextView that is called DictionaryConsole. Every 0.05 seconds, text is being set to it based on the following switch statement:

 switch ((DictionaryLine*100)/NumberofWordsTilEnd) {
    case 10:
        NewLine = [NSString stringWithFormat: @"Attack is Now 10 Percent Complete"];
        NewLine2 = [NewLine stringByAppendingString:@"\n"];
        DictionaryConsole.text = [NewLine2 stringByAppendingString:DictionaryConsole.text];
        break;
    case 25:
        NewLine = [NSString stringWithFormat: @"Attack is Now 25 Percent Complete"];
        NewLine2 = [NewLine stringByAppendingString:@"\n"];
        DictionaryConsole.text = [NewLine2 stringByAppendingString:DictionaryConsole.text];
        break;
    case 50:
        NewLine = [NSString stringWithFormat: @"Attack is Now 50 Percent Complete"];
        NewLine2 = [NewLine stringByAppendingString:@"\n"];
        DictionaryConsole.text = [NewLine2 stringByAppendingString:DictionaryConsole.text];
        break;
    case 75:
        NewLine = [NSString stringWithFormat: @"Attack is Now 75 Percent Complete"];
        NewLine2 = [NewLine stringByAppendingString:@"\n"];
        DictionaryConsole.text = [NewLine2 stringByAppendingString:DictionaryConsole.text];
        break;
    case 80:
        NewLine = [NSString stringWithFormat: @"Attack is Now 80 Percent Complete"];
        NewLine2 = [NewLine stringByAppendingString:@"\n"];
        DictionaryConsole.text = [NewLine2 stringByAppendingString:DictionaryConsole.text];
        break;
    case 90:
        NewLine = [NSString stringWithFormat: @"Attack is Now 90 Percent Complete"];
        NewLine2 = [NewLine stringByAppendingString:@"\n"];
        DictionaryConsole.text = [NewLine2 stringByAppendingString:DictionaryConsole.text];
        break;
    case 95:
        NewLine = [NSString stringWithFormat: @"Attack is Now 95 Percent Complete"];
        NewLine2 = [NewLine stringByAppendingString:@"\n"];
        DictionaryConsole.text = [NewLine2 stringByAppendingString:DictionaryConsole.text];
    case 98:
        NewLine = [NSString stringWithFormat: @"Attack is Now 98 Percent Complete"];
        NewLine2 = [NewLine stringByAppendingString:@"\n"];
        DictionaryConsole.text = [NewLine2 stringByAppendingString:DictionaryConsole.text];
        break;
    default:
        break;


}

This is the NSTimer:

 Dictionary = [NSTimer scheduledTimerWithTimeInterval:0.05 
                                               target:self     
                                             selector:@selector(DictionaryAttack) 
                                             userInfo:nil 
                                              repeats:YES];

Every time -(void)DictionaryAttack is called, that switch statement gets executed. Dictionary Line is an int that is increased by 1 in -(void)DictionaryAttack. The strings go to the DictionaryConsole(UITextView) with no problem. But my problems is 33 of the NewLine string is "printed" to the text view. However, I would only like one string to be "printed". What did I do wrong?

Yuchen
  • 30,852
  • 26
  • 164
  • 234
TonyStark
  • 96
  • 1
  • 12

1 Answers1

1

You are trying to divide integers. The result will be a truncated number, which may fit your switch case multiple times. Ex: 10.0 will truncate to 10, and so will 10.1, so your switch case 10 code will execute multiple times.

What is the behavior of integer division?

Community
  • 1
  • 1
ozz
  • 1,137
  • 7
  • 9
  • So can I make it so it's not rounded? – TonyStark Feb 08 '15 at 13:47
  • Nevermind, I ended up adding an if statement to each case to check and see if an integer was set to 1, and increased it to 2 after the string had been "printed" to the UITextView, so that the next time case 10 occurred, it would not execute because the integer had a new value. Thank you for telling me what went wrong, though, it helped a lot. – TonyStark Feb 08 '15 at 17:32
  • Sure thing. Keep this in mind whenever you are doing division in C, because it is very easy to create bugs if you are not clear about the math behavior of various data types. – ozz Feb 08 '15 at 21:54