The final word on NSStrings: Mutable and Immutable

后端 未结 6 609
忘掉有多难
忘掉有多难 2020-12-16 23:14

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\".)

相关标签:
6条回答
  • 2020-12-17 00:02

    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.

    0 讨论(0)
  • 2020-12-17 00:06

    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.

    0 讨论(0)
  • 2020-12-17 00:07

    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];
    
    0 讨论(0)
  • 2020-12-17 00:11

    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).

    0 讨论(0)
  • 2020-12-17 00:17

    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.

    0 讨论(0)
  • 2020-12-17 00:20

    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.

    0 讨论(0)
提交回复
热议问题