Search code examples
iosmemory-leaksnsstringautomatic-ref-countingnscopying

In ios will NSString = NSString cause memory leaks?


I've seen some answers but those didn't expand enough and I think they were before ARC was introduced.

So if I have two NSSstring can I set

string1 =string2;

or

NSString * a1=@"String";
al=@"Lead";

without causing memory leaks or other problems?

Edit: What about view controller or delegate properties set to (copy, nonatomic)?

Edit2: It should be NSString *al=@"String". I hope this doesn't change the idea that it doesn't leak.


Solution

  • neither would leak in ARC.

    however, you might want to be aware that you are dealing with a reference in this case -- no implicit copy is performed (unless you use a copy or copy-qualified property).

    Edit: What about view controller or delegate properties set to (copy, nonatomic)?

    for NSStrings? yes, copy should be the default for NSStrings. nonatomic should be the default for everything. of course, there are exceptions to this -- when you might deviate from the defaults.