Creating an NSString in a C Object

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

There are two ways to create an NSString obj object in Objective-C

Method 1:

// string1 will be released automatically NSString* string1 = [NSString string]; // must release this when done 

Way 2

 NSString* string2 = [[NSString alloc] init]; [string2 release]; 

If i do

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

My question is, in what direction is the declaration above, and we must finally let it go

+4
source share
4 answers

String constants should not be freed; they cannot be explicitly released by any self-realized; they are just constants and never freed.

So, just-select them if you saved them before.

+8
source

in the first and last, you do not own (select and initialize) the line, so do not release them. while in the second you selected it manually, so you need to free it. if you use objects in your code, you must use the second, the other use the first or last

+2
source

if you use ARC, you shouldn't have a release huff at all just put string = nil; however no you don't need to release this

+2
source

if you alloc it, then you should release it.

Lines

created using a static method of type

 [NSString stringWithXXXX]; 

are auto released

@ H2CO3 is valid with respect to constants.

+1
source

Source: https://habr.com/ru/post/1411112/


All Articles