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