NSString stringWithString VS @ "string"

I was curious if there is a difference between the following two codes?

NSString * aString = [NSString stringWithString: @ "string"];

NSString * aString = @ "string";

I wonder what exactly happens when you do the last method.

+4
source share
1 answer

Both point to a literal string created at compile time.

Even though stringWithString offers it autoreleased, a literal string will never be released.

See my sister post here:

Difference between NSString Literals

From Apple Docs @ https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/CreatingStrings.html

Such an object is created at compile time and exists throughout your program execution. The compiler makes such object constants unique on the basis of each module, and theyre never freed, although you can save and release them like any other object.

+3
source

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


All Articles