Is a literal NSString autoreleased or does it need to be released?

Coocoo4Cocoa picture Coocoo4Cocoa · Dec 1, 2008 · Viewed 7.7k times · Source

When creating a string using the following notation:

NSString *foo = @"Bar";

Does one need to release foo? Or is foo autoreleased in this case?

Answer

Ben Gottlieb picture Ben Gottlieb · Dec 1, 2008

Compiler allocated strings (of the format @"STRING") are constant, and so -retain, -release, and -autorelease messages to them are ignored. You don't have to release or autorelease foo in this case (but it won't hurt).