NSString stringWithformat: returns c-string?


#1

Hey, I’m loving the book and working my way through it… so far memory management is the only tricky part but it’s slowly coming around. I have one question. On page 55, rather than make an NSString via initWithformat:, you say use stringWithFormat: which is a convenience method that returns an NSString that is ready to autorelease.

However, I took a close look at the documentation (developer.apple.com/library/mac/ … tring.html) and it seems like initWithFormat: returns an NSString (object) while stringWithFormat: returns a simple c-string (which never really needs its memory managed). Is that right?

Not trying to be picky here, just want to be sure I really understand what’s going on.


#2

Hi,

Both of these do pretty much the same thing but initWithFormat is an instance method that would need an alloc-ed instance of NSString - therefore you are responsible for releasing it.

stringWithFormat is a class method - it creates and initializes a new NSString and then autoreleases it so you don’t have to release it.

Gareth