0

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

3

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

Dr. Acula
  • 2,392
  • 4
  • 17
  • 17
1

Use subStringToIndex method instead of subStringFromIndex

NSString *temp = [string substringToIndex:length-1];
KingofBliss
  • 15,055
  • 6
  • 50
  • 72
  • +1 for editing the post AND getting the right answer in within 20 seconds of the other guy :) – Steve Jul 06 '11 at 04:31