stringWithFormat or initWithFormat ?

Nibbie
Posts: 4
Joined: 2009.10
Post: #1
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 ?
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
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.
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #3
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.
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #4
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.
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #5
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...
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #6
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...
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Memory Leak using stringWithFormat MikeD 6 6,009 Apr 13, 2009 08:30 AM
Last Post: MikeD