iDevGames Forums
stringWithFormat or initWithFormat ? - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: stringWithFormat or initWithFormat ? (/thread-680.html)



stringWithFormat or initWithFormat ? - Bracer - Oct 13, 2009 03:11 AM

Code:
NSMutableString* variable = [NSMutableString stringWithFormat:@"%d %d %d",1,2,3];
Code:
NSMutableString* variable = [ [NSMutableString alloc] initWithFormat:@"%d %d %d",1,2,3];

Is there any advantage the second example have over the first accept for the fact that you'll have to release it manually later ?


stringWithFormat or initWithFormat ? - ThemsAllTook - Oct 13, 2009 06:53 AM

If you're going to be retaining your string, or you want to keep it out of the autorelease pool and release it yourself, using -initWithFormat: makes more sense. If you're going to be autoreleasing your string anyway, using +stringWithFormat: makes more sense. The only difference is whether the returned object is autoreleased or not.


stringWithFormat or initWithFormat ? - AnotherJake - Oct 13, 2009 06:55 AM

I don't think there's any advantage/disadvantage either way in terms of performance (if there is, it'd be negligible). Yeah, autorelease and a shorter statement is the only real difference.


stringWithFormat or initWithFormat ? - longjumper - Oct 13, 2009 10:26 AM

If you are just creating one object, it doesn't really matter. For objects that are created, used and disposed of locally, use the convenience method to create the object.

However, when you start creating lots and lots of objects, there will be a difference. If you're running through a loop a bunch of times and creating a temporary object within the scope of that loop, you will want to alloc/release it.

Furthermore, stuff like this:
Code:
str = [[NSString stringWithFormat:@"hey hey"] retain];

Is actually less efficient than:
Code:
str = [[NSString alloc] initWithFormat:@"hey hey"];

Why? There is actually no retainCount instance variable for objects. When an object is created (and therefore has a "retain count" of 1), it is just an object sitting somewhere in memory. When an object's retain count is greater than 1, it is added to a map of objects that have more than one owner. Their retain count is stored by this map, not by the object itself. When the retain count for an object drops below 2, it is removed from this map. When an object is sent release and it does not belong to this map, it deallocates itself.

So, by creating an autoreleased object and then retaining it, you add and remove from the map pointlessly. Of course, there are times when this can't be avoided, but if you have the choice, don't autorelease/immediately retain.


stringWithFormat or initWithFormat ? - AnotherJake - Oct 13, 2009 11:58 AM

That last point is very interesting, LongJumper! I didn't know about that map for objects of more than 1 retaining object. I'll have to remember that one...


stringWithFormat or initWithFormat ? - ThemsAllTook - Oct 13, 2009 01:24 PM

Yeah, that was a surprise for me, too. I went and found the NSObject source code, and sure enough, it does appear to be in a separate map. You learn something new every day...