The final word on NSStrings: Mutable and Immutable

随声附和 提交于 2019-12-29 06:19:02

问题


I've read in several books... and online... about immutable and mutable strings. They claim "immutable strings" can't be changed. (But they never define "change".)

Which of these NSStrings could be changed without using NSMutableString?

The string contains "catfish"... and I later try to change it to "cat". (Same letters, just shorter.)

It contains "cat"... and I try to change it to "catfish". (Similar start... but just made longer.)

I change "cat" into "CAT". (Same letters, but just the case has changed.)

I change "cat" into "dog". (Totally different string, but the same length.)

I change "cat" into "dogwood". (Totally different string, but longer.)


回答1:


The answer is - none. Immutable means it cannot be changed into anything different than the original string. Every character stays the same, the length cannot change, etc. Once it's defined you cannot change anything about that string. If you want to make it longer or shorter or change any of the characters, you need to make a new string (or use a NSMutableString to begin with).




回答2:


If I declare a variable:

NSString * var;
// Here some code giving a new value to var

What is immutable is the object pointed to by var, not var itself.

This means that I cannot change anything of the object pointed to by var, but I can change which object is pointed to.

None of the operations you mention is allowed on var, but you can assign var with another different string:

NSString * anotherVar;
// Here some code giving a new value to anotherVar

var = anotherVar; // This is allowed (with a memory leak if GC is disabled)

// The following is also allowed: -stringWithFormat: creates a new object
var = [var stringWithFormat:@"%@ with more chars", var];



回答3:


NSString cannot be edited, every time you assign a string value to NSString object it will orphan the old string object and re-create the new string object. That is an expensive operation if you are dealing with large string buffer. If you are dealing with large string buffer then use NSMutableString for optimum performance. This is similar to string vs stringBuilder in .net.




回答4:


You need to define "change", too :)

Basically, when you create an NSString, you're creating an array of characters and you're telling the compiler that the contents of said array will never change. The "never change" part is the "immutable" part; the data the string contains can't ever be modified.

On the other hand, an NSMutableString allows you to actually change the data it points to (hence it's 'mutable'). Note the 'appendString' method that exists for NSMutableString; this actually takes data and slaps it onto the end of the string.

For example, if you had:

NSString *myString = @"Cat"; myString = @"Goldfish";

That would work fine. You're never actually changing the data myString contains; you're simply telling it that you want it to point to a new segment of unchangeable data.

The only time you'd actually run into problems with the differences between mutable and immutable objects is if you tried to actually modify myString directly (e.g. append "s are cool" to it or something).

In other words, either class would allow you to "change" any string into any other string, but the methods you used to do it would be very different. For example, to turn "cat" into "CAT" with a mutable string, you could iterate over every character and simply make it uppercase (since you can modify the contents of a mutable string). With an immutable string, on the other hand, you'd have to first copy the original string elsewhere, modify it, and then redirect your variable to point to this new NSString.

The following links might be useful: http://www.kirupa.com/forum/showthread.php?t=307928 http://forums.macrumors.com/showthread.php?t=467099

I'd also recommend doing some googling; this is a question that relates to development in general, and it's a relatively important concept to grasp. It's also been discussed to death elsewhere on the internet.




回答5:


An immutable string (or indeed, any immutable object) is one that can't be changed at all after initialisation.

Here's the advantage of using immutable strings, in (pseudo-)pseudo-code:

// if using mutable strings
let a = "Hello World";
let b = a;
a.replace("Hello", "Goodbye");

Now what does a hold? "Hello World"? "Goodbye World"? Now what does b hold?

If strings are mutable, then it might hold "Goodbye World". This may be what you want. It might not be.

With immutability, it becomes very clear that b still points to the original string, because the replace method can't have changed the actual string, but rather returned a reference to a new string.

You might not see much benefit in this example. But if a and b are referenced and used in different parts of the code, or in different threads, having immutable references removes a lot of potential errors.




回答6:


By default, NSString used in objective C language is immutable. NSMutableString is used to declare & define a mutable string. Nothing in an immutable string can be changed. Not the length, not the characters, nothing.



来源:https://stackoverflow.com/questions/905396/the-final-word-on-nsstrings-mutable-and-immutable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!