Quote:
Originally Posted by RickSDK
by the way, you can avoid the alloc by using StringWithFormat:
NSString *parsedText = [NSString StringWithFormat:@"%0.2f", num1];
|
To say you can "avoid the alloc" is a little misleading.
stringWithFormat is a "convenience method." It creates and returns an autoreleased string object. It would be more accurate to say that you can avoid having to release the string after using it.
Internally, the implementation of stringWithFormat probably looks something like this:
Code:
+ (id)stringWithFormat:(NSString *)format, ...
{
result = [NSString alloc] initWithFormat: format, ...];
return [result autorelease];
}
(It won't be exactly like that, because there's some magic I don't remember that you have to do to pass the variable number of arguments on to initWithFormat, but you get the idea.)
The OPs code, using initWithFormat, had a memory leak unless he was using ARC.
In manual reference counting, after you alloc/init and object, you have to release it when you are done with it.
stringWithFormat returns an autoreleased string, so you don't have to release it.