Alright, this is an elementary question but I'm asking because I honestly don't understand how to properly manage this. If I uncomment the last two lines, this code crashes, even though I don't think it should.
The following code is from a custom subclassed UILabel where I added the following method, setTextFromFloat.
-(void)setTextFromFloat:(float)newValue {
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:2];
[formatter setRoundingMode:NSNumberFormatterRoundUp];
NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:newValue]];
NSString *newLabelValue = [numberString stringByAppendingString:@"x"];
self.text = newLabelValue;
//[numberString release];
//[formatter release];
}
So, there are three object here that I am confused about:
a) self.text (the old string value) - When is this released? Should I release the old contents of self.text when I call this function?
b) formatter, the NSNumberFormatter I alloc'd here almost certainly needs release
c) what about numberString? I just use it as an intermediary to build newLabelValue. if I release it I believe the program crashes, but why? Am I not responsible for the memory used indirectly by calling stringFromNumber?
Any wisdom greatly appreciated, thank you!