Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to release NSString generated using @"..."?

Tags:

objective-c

If I make an NSString using the code below, do I need to need to release someString?

NSString *someString = @"somestring";
like image 317
Sam Lee Avatar asked Mar 26 '09 23:03

Sam Lee


2 Answers

No, it's a compile time constant string object, so it doesn't need releasing. It's the moral equiv of char *c = "hello world" -- where the string hello world is in global data, and you're assigning the address of this data to the pointer c.

like image 198
Brian Mitchell Avatar answered Sep 28 '22 03:09

Brian Mitchell


If you created an object via a method call that contains alloc, retain, or copy, or starts with new (N-A-R-C = "narc"), then you are responsible for releasing the object. If this is not the case, then you can ignore the object.

So in the case of strings:

NSString * myString = @"This is a string";

I don't see a call there to a NARC method, so you are not responsible for releasing it. It's really that simple.

like image 29
Dave DeLong Avatar answered Sep 28 '22 01:09

Dave DeLong