Book: Objective-C Developer reference, Wiley Pub, 2011, ISBN: 978-0-470-47922-3 Author: Jiva DeVoe
Chap: 12 Page 265 'Using Strings'
Ok, so it says: "Any two declarations of the exact same string value, even if stored in different variable names, point to the same object."
So I do this:
string1 = Number One
string2 = Number OneNew Text
If string1 and string2 point to the SAME object, then why when one is changed, the other doesn't change?
And how in the world do they both point to the same object only based on string content?
Or is it that the pointers WERE the same, and now that one string changed, the pointer changed? if so, that's just strange.
Edit: Ok, just checked, with this code:
They are == on the 1st if() and NOT == on the second if(), so changing a string, changes it's pointer and if two strings are the same, they'll be given the same pointer?
Edit2:
Ok, so what happens if I have two strings NOT the same, then within the program, they become the same, have I just lost a pointer? And what happens if that pointer was used specifically in another part or another thread of the same program? What if I store the value of a pointer to a var, the pointer then changes... does the var change?
Chap: 12 Page 265 'Using Strings'
Any two declarations of the exact same string value, even if stored
in different variable names, point to the same object. Therefore,
you can use these strings for keys, for example, where the equality
of the string as compared to another instance of that string will
be considered to be equal, both using the -isEqual: object
method as well as the == operator, which will compare the value
of the pointers.
Code:
Listing 12.2
Examining string constant equality
NSString *string1 = @”this is a string”;
NSString *string2 = @”this is a string”; // same object as string1
NSString *string3 = [NSString stringWithString:string1]; // makes new
assert(string1 == string2); // true
assert([string1 isEqual:string2]); // also true
assert([string1 isEqual:string3]); // true
assert(string1 == string3); // false
Ok, so it says: "Any two declarations of the exact same string value, even if stored in different variable names, point to the same object."
So I do this:
Code:
NSString *string1 = @"Number One";
NSString *string2 = @"Number One";
string2 = [string2 stringByAppendingString: @"New Text"];
NSLog(@"String 1 %@", string1);
NSLog(@"String 2 %@", string2);
string1 = Number One
string2 = Number OneNew Text
If string1 and string2 point to the SAME object, then why when one is changed, the other doesn't change?
And how in the world do they both point to the same object only based on string content?
Or is it that the pointers WERE the same, and now that one string changed, the pointer changed? if so, that's just strange.
Edit: Ok, just checked, with this code:
NSString *string1 = @"Number One";
NSString *string2 = @"Number One";
if (string1 == string2)
NSLog(@"Strings are ==");
string2 = [string2 stringByAppendingString: @"New Text"];
if (string1 == string2)
NSLog(@"Strings are STILL ==");
NSLog(@"String 1 %@", string1);
NSLog(@"String 2 %@", string2);
They are == on the 1st if() and NOT == on the second if(), so changing a string, changes it's pointer and if two strings are the same, they'll be given the same pointer?
Edit2:
Ok, so what happens if I have two strings NOT the same, then within the program, they become the same, have I just lost a pointer? And what happens if that pointer was used specifically in another part or another thread of the same program? What if I store the value of a pointer to a var, the pointer then changes... does the var change?
Last edited: