简体   繁体   中英

is NSString value cannot be changed once it is provided a value?

Hi I got an information that NSString value is immutable and cannot be changed once it is provided a value.

But I have created and tested the following code:

NSString *str=[[NSString alloc] initWithString:@"Hello"];    
NSLog(@"\n\nstr = %@",str);
str=@"asdasd";
NSLog(@"\n\n new str = %@",str);

and this gives "Hello" as first str value and "asdasd" as second str value. If it is so what is the relevance of calling NSString immutable?Thanks in advance.

What you're doing on the third line is just creating a new string and pointing it with the str variable. You didn't change the original string, but rather made the variable point to a new one.

NSString is immutable. You cannot mutate it. Your confusion arises from the fact that your variable (pointer) is not const .

All you're doing is reassigning the pointer to a different immutable string.

If you were to attempt to append a string to either, then an error would ensue. This, specifically, is "…the relevance of calling NSString immutable". You should interpret this as the string instance may not be mutated, even though the variable you have declared may be assigned another NSString instance because it is not const .

You can make both the pointer const and the string immutable like so:

NSString * const aStr = @"aStr";

You're confusing an immutable object and a constant pointer (plus you're leaking memory). Here you don't really change the NSString instance (that is, the object itself), but just a pointer to it. The point is that you can assign different instances of an NSString to the same variable, it won't change the internal contents of the object, nor would it make it respond to NSMutableString's mutation messages such as appendString etc.

Use this will sol your problem...

NSString *str=[NSString stringWithFormat:@"Hello"];    
NSLog(@"\n\nstr = %@",str);
str = nil;
str=@"asdasd";
NSLog(@"\n\n new str = %@",str);

This some basic that you should know..

NSString *s1 = @"string1";
NSString *s2 = [[NSString alloc] initWithString:@"string2"];
NSString *s3 = [NSString stringWithFormat:@"string3"];

s1 in this case is a pointer to a constant string. s2 and s3 both point to new strings that you've created, but s2 has been retained for you, and s3 has been autoreleased. If you just need a temporary object, the autoreleased object s3 or the constant object s1 are good choices. If you need to keep the string around, you should use s2 (actually now that I think about it s1 will work in this case too - it's not really idiomatic, though).

You can make s1 or s3 equivalent to s2 by sending them a retain message. You could also turn s2 into an equivalent object by sending it an autorelease message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM