Difference between NSString Literals

What is the difference between these two lines?

NSString * string = @"My String"; NSString * string = [[[NSString alloc] initWithString:@"MyString"] autorelease] 
+14
objective-c cocoa nsstring
Nov 07 '11 at 3:12
source share
5 answers

@ "My String" is a literal string compiled into a binary file. When loading, it has a place in memory. The first line declares a variable pointing to this point in memory.

From the string programming guide:

The easiest way to create a string object in your source code is to use the Objective-C @ "..." construct:

 NSString *temp = @"/tmp/scratch"; 

Note that when creating a string constant in this way, you should avoid using anything other than 7-bit ASCII characters. Such an object is created at compile time and exists at runtime of your programs. The compiler makes such an object constants unique to each module, and they are never freed, although you can save and free them like any other object.

The second line selects the line, taking this literal line. Note that the literal strings "My String" are the same. To prove this:

 NSString *str = @"My String"; NSLog(@"%@ (%p)", str, str); NSString *str2 = [[NSString alloc] initWithString:@"My String"]; NSLog(@"%@ (%p)", str2, str2); NSString *copy = [str2 stringByAppendingString:@"2"]; NSLog(@"%@ (%p)", copy, copy); 

Prints the same memory address:

 2011-11-07 07:11:26.172 Craplet[5433:707] My String (0x100002268) 2011-11-07 07:11:26.174 Craplet[5433:707] My String (0x100002268) 2011-11-07 07:11:26.174 Craplet[5433:707] My String2 (0x1003002a0) 

What it says is not only the first two lines of the same memory address, but if you do not change the code, it is the same memory address every time you run it. This is the same binary offset in memory. But not only the copy is different, but also different each time it is launched, as it stands out on the heap.

Abstract does not affect the above doc document. You can set them free, but they are never set free. Thus, they are not equal because both are auto-implemented strings, but they are both constants and the release is ignored.

+22
Nov 07 '11 at 3:18
source share

One is the literal string that is saved for the life of the executable application. Another may be a dynamic object, which is saved only until auto-installation. (This may be a literal string, if the system decides to optimize it this way - there are no guarantees, it will not.)

+6
Nov 07 '11 at 4:16
source share

bryanmac is 100% right in his answer. I just added an explicit example using GHUnit .

NSString creation - literal vs nonliteral.

Shows strings created in different ways if they are literal or non-literal.

 - (void) test_stringCreation { NSString *literalString = @"literalString"; NSString *referenced = literalString; NSString *copy = [literalString copy]; NSString *initWithString = [[NSString alloc] initWithString:literalString]; NSString *initWithFormat = [[NSString alloc] initWithFormat:@"%@", literalString]; // Testing that the memory addresses of referenced objects are the same. GHAssertEquals(literalString, @"literalString", @"literal"); GHAssertEquals(referenced, @"literalString", @"literal"); GHAssertEquals(copy, @"literalString", @"literal"); GHAssertEquals(initWithString, @"literalString", @"literal"); GHAssertNotEquals(initWithFormat, @"literalString", @"nonliteral - referenced objects' memory addresses are \ different."); // Testing that the objects referenced are equal, ie isEqual: . GHAssertEqualObjects(literalString, @"literalString", nil); GHAssertEqualObjects(referenced, @"literalString", nil); GHAssertEqualObjects(copy, @"literalString", nil); GHAssertEqualObjects(initWithString, @"literalString", nil); GHAssertEqualObjects(initWithFormat, @"literalString", nil); // Testing that the strings referenced are the same, ie isEqualToString: . GHAssertEqualStrings(literalString, @"literalString", nil); GHAssertEqualStrings(referenced, @"literalString", nil); GHAssertEqualStrings(copy, @"literalString", nil); GHAssertEqualStrings(initWithString, @"literalString", nil); GHAssertEqualStrings(initWithFormat, @"literalString", nil); } 
+2
Apr 04 '13 at 20:13
source share

There is no difference between them. The line initiated by the one you showed in the first example is a line with auto-implementation.

+1
Nov 07 '11 at 3:19
source share

Just remember this basic thing: -

 NSString *string = ... 

This is a pointer to an object, not an object!

Therefore, the operator: NSString *string = @"Hello"; assigns the address of the @"Hello" object to the pointer string.

@"Hello" interpreted as a constant string by the compiler, and the compiler itself allocates memory for it.

Similarly, the statute

 NSObject *myObject = somethingElse; 

assigns the address somethingElse to the pointer myObject and that somethingElse should already be designated as the initiator of the declaration.

Therefore, the statement: NSObject *myObject = [[NSObject alloc] init]; selects and initializes the NSObject and assigns its address myObject .

0
Mar 31 '13 at 19:03
source share



All Articles