Skip navigation
This discussion is archived

NSString: Differences between initWithFormat and stringWithFormat

11732 Views 5 Replies Latest reply: Oct 20, 2008 4:07 AM by just.do.it RSS
wallclimber21 Level 1 Level 1 (0 points)
Currently Being Moderated
Sep 1, 2008 10:45 PM
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)
  • Brent Royal-Gordon Level 2 Level 2 (335 points)
    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.
    MacBook, Mac OS X (10.5.4), iPhone EDGE 16GB
  • Brent Royal-Gordon Level 2 Level 2 (335 points)
    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.)
    MacBook, Mac OS X (10.5.4), iPhone EDGE 16GB
  • rongreen Calculating status...
    Yes, but neither of these methods contain any of the NARC words. So the question remains how would you know this?
    20" Intel iMac, Mac OS X (10.4.6)
  • just.do.it Level 3 Level 3 (840 points)
    Yes, that's correct, but initWithFormat without an alloc in front does not make any sense. In fact it's the alloc message, which tell's us, the returned object is retained.
    MacBook Pro, Mac OS X (10.5.5)

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.