I’m a complete novice, so I’m probably missing something really easy, but I can’t get my string appending to work. I add the 3rd character to typedDigit & it crashes – the method is called fine and typedDigit will get to 2 characters long. I think everything is declared properly in the header file. Code is –
-(IBAction)digitPressed:(UIButton *)sender {
NSString *digit = [[sender titleLabel] text]; // in this case, "0" - "9"
if (userIsInMiddleOfTyping) { // typedDigit is already at least 1 character long
typedDigit = [typedDigit stringByAppendingString:digit];
} else { // first character of typedDigit
typedDigit = digit;
userIsInMiddleOfTyping = YES;
}
}
Many thanks for any help!
Without the stack trace of the crash, it’s hard to know the cause, but my guess will be that
typedDigitis being autoreleased before the next call of yourdigitPressedfunction.stringByAppendingString:returns an autoreleased object, so you’ll need toretainit if you want it to hand around past the next autorelease pool flush. For a direct fix, try something like…More than this, you’ll need to make sure you
releasetypedDigit at some point after the typing is over, and you’re finished with it.