NSString: Differences between initWithFormat and stringWithFormat

This feels like a really dumb question, but the docs aren't exactly clear about this: how are they different? Is stringWithFormat simply a wrapper around initWithFormat that does an additional alloc / autorelease?

This actually came up in the following: I have a piece of code that creates an identifier string for an object:

Like this:

+(NSString *)createDictionaryKeyFromABC:(int)a b:(int)b c:(int)c
{
NSString *key = [[NSString alloc] initWithFormat: @"%d:%d:%d", a, b, c];
return key;
}



Usually, I only need to use those elements for a short time, so I find myself doing ... release right after usage. Is this a place where stringWithFormat would be better?

Thanks,
Tom

PC, Mac OS X (10.5.4)

Posted on Sep 1, 2008 10:31 PM

Reply
5 replies

Sep 1, 2008 10:57 PM in response to wallclimber21

The difference is in how the return values are memory-managed. alloc/initWithFormat: returns a retained string that you will have to release or autorelease yourself. stringWithFormat: returns a string that has already been autoreleased. (An autoreleased object will be released when the current autorelease pool disappears; that usually happens right before the next event is processed.)

Which you should use depends on what you're doing with the string. If it's a temporary string, if it's going to be stored inside another object, or if it's going to be stored in your own object using the "self.property" syntax, use stringWithFormat:; if it's going to be assigned to a global, static or instance variable (without using "self.property"), use alloc/initWithFormat:.

In your case, you could go either way. stringWithFormat: would allow you to remove the explicit release call, but autoreleasing is also very slightly slower than releasing. It's up to you to decide whether you want your code to be smaller and simpler or longer and faster.

Sep 1, 2008 11:01 PM in response to wallclimber21

The docs for NSString don't say this because it's a Cocoa-wide rule, and Apple doesn't want to explain the memory management rules in the documentation for every single method. :^) The rule is, if the method contains one of the words "new", "alloc", "retain", or "copy" (NARC), you have to release its return value yourself. If it doesn't--and this applies to all these sorts of shorthand constructors--then you don't have to release it yourself. That's always true everywhere in Cocoa, and you should try to make sure it's true in your code too. (So if you decide to use explicit releases, I'd suggest you add one of those words to your method name--maybe call it "newDictionaryKeyFromABC", for example.)

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

NSString: Differences between initWithFormat and stringWithFormat

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.