Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

objective c - NSString immutable allows to change its values?

The code below compiles and runs, BUT according to all iPhone development books and Apple documentation it shouldnt! Can someone please explain to me how come immutable NSString allows to change its values after it has been set? I thought I had to use NSMuttableString to change context of the same string variable? I am using SDK 3.1.

NSString *test =[[NSString alloc] initWithString:@"TEST"];
[test release];
test = @"TEST2";
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Perhaps the following example will add upon the replies from Mark and Niels and help clarify things.

Immutable strings

// Setup two variables to point to the same string
NSString * str1 = @"Hello World";
NSString * str2 = str1;

// "Replace" the second string
str2 = @"Hello ikilimnik";

// And list their current values
NSLog(@"str1 = %@, str2 = %@", str1, str2);

Mutable strings

// Setup two variables to point to the same string
NSMutableString * str1 = [NSMutableString stringWithString:@"Hello World"];
NSMutableString * str2 = str1;

// "Replace" the second string
[str2 setString:@"Hello ikilimnik"];

// And list their current values
NSLog(@"str1 = %@, str2 = %@", str1, str2);

Notice when you use the immutable NSString class that the only way to "replace" a string is to create a new string and update your variable "str2" to point to it. This however doesn't affect what "str1" is pointing to, so it will still reference the original string.

In the NSMutableString example, we don't create a second string, but instead alter (mutate) the contents of the existing "Hello World" string. Since both variables continue to point to the same string object, they will both report the new value in the call to NSLog.

It's important to differentiate between a pointer variable and the actual object it points to. A NSString object is immutable, but that doesn't stop you from changing the value of a variable which points to a string.

The data type "NSString *" is a pointer to a NSString object, not the object itself. If you set a break point at either of the NSLog statements within the XCode debugger, you may like to check the raw value of each variable to clarify this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...