Reputation: 3534
I would like to get a clarification/difference on the NSString declaration. Consider the following codes:
NSString *str = @"string";
NSString *str = [NSString stringWithFormat:@"string"];
NSString *str = [[NSString alloc] initWithString:@"string"];
Can anyone help me to understand the difference between the above three type of string declaration? Does the difference come in terms of memory or will there be any other reasons? I have gone through various posts to understand the difference, but I couldn't understand the exact difference.
Sree
Upvotes: 1
Views: 99
Reputation: 81878
@"string"
This will make the compiler emit a statically allocated NSString
instance. The object itself will never be deallocated (release
is a no-op).
[NSString stringWithFormat:@"string"]
Here the string is created from parsing and transforming the format string. This involves runtime overhead and returns an autoreleased instance.
[[NSString alloc] initWithString:@"string"]
This will (1) create the literal, (2) allocate a default string, (3) throw away the default string, (4) call copy on the literal, and (5) return the result (again, the literal, because copy just returns it). The logic retain count is +1 (automatically handled by ARC).
Upvotes: 1
Reputation: 14073
Edit (thanks for the comments):
Using ARC, the first statement is used by the compiler to create a string that is accessible during the lifetime of the app and never deallocated. The last two statements produce the same kind of string.
Using manual memory management, the second statement produces an autoreleased string. The last produces a retained string. This means, when using the last statement, you would have to add a release later in the code.
Upvotes: 1
Reputation: 1557
as I remember in this case NSString *str = @"string"; the memory for the string will be allocated once and will be reused for all same strings in whole application. Something like global constant. https://en.wikipedia.org/wiki/String_literal
There is the proof: "Objective-C string constant is created at compile time and exists throughout your program’s execution. The compiler makes such object constants unique on a per-module basis, and they’re never deallocated" https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Strings/Articles/CreatingStrings.html
The others 2 is the same, also you can formate string with stringWithFormat method: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Strings/Articles/FormatStrings.html#//apple_ref/doc/uid/20000943
Upvotes: 1
Reputation: 1097
for: NSString *str = @"string";
used when you use as a static string .
for Exmple.
int abc=5;
for: NSString *str = [NSString stringWithFormat:@"%d",abc];
used when you convert your integer or float into string.
for: NSString *str = [[NSString alloc] initWithString:@"string"];
used when above same same reason but only difference is you alloc string and then passes static string.
but in ARC no need to alloc string.
Upvotes: 1