正在设置为UITextView的文本被多次设置

时间:2015-02-07 19:14:10

标签: objective-c xcode nstimer

我有一个名为DictionaryConsole的UITextView。每隔0.05秒,基于以下switch语句将文本设置为它:

 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;


}

这是NSTimer

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

每次调用-(void)DictionaryAttack时,都会执行该switch语句。 Dictionary Line是一个在-(void)DictionaryAttack中增加1的int。字符串可以毫无问题地转到DictionaryConsoleUITextView)。但是我的问题是NewLine字符串中的33个是"打印"到文本视图。但是,我只想要一个字符串"打印"。我做错了什么?

1 个答案:

答案 0 :(得分:1)

你试图划分整数。结果将是截断数字,可能多次适合您的开关案例。例如:10.0将截断为1010.1也会截断,因此您的切换case 10代码将多次执行。

What is the behavior of integer division?