Creating backspace on iOS calculator

2019-04-02 18:41发布

问题:

I'm trying to add a backspace button to remove the last entered digit in a calculator, but I can't seem to get it. Here is what I've tried:

-(IBAction)backspacePressed:(UIButton *)sender {

    NSMutableString *string = (NSMutableString*)[display text];

    int length = [string length];

    NSString *temp = [string substringFromIndex:length-1];

    [display setText:[NSString stringWithFormat:@"%@",temp]];

}

Any ideas?

回答1:

I think you want [string substringToIndex:length-1] instead of [string substringFromIndex:length-1].



回答2:

Use subStringToIndex method instead of subStringFromIndex

NSString *temp = [string substringToIndex:length-1];