问题
This question already has an answer here:
- What is the purpose of having both NSMutableString and NSString? 4 answers
- What is the difference between NSString and NSMutableString? [duplicate] 4 answers
what is actual difference between NSString and NSMutable String in Objective-C? I have searched a lot but not getting any answer..I understand that NSString is Immutable object and NSMutableString is Mutable object but I want to understand the difference between them with the help of an example..Please help me..
now question has solved..
回答1:
Immutable String.....
NSString *str1 = @"Hello Testing";
NSString *str2 = str1;
replace the second string
str2 = @"Hello hehehe";
And list their current values
NSLog(@"str1 = %@, str2 = %@", str1, str2);
//logs as below
//str1 = Hello Testing, str2 = Hello hehehe
Mutable strings
Setup two variables to point to the same string
NSMutableString * str1 = [NSMutableString stringWithString:@"Hello Testing"];
NSMutableString * str2 = str1;
Replace the second string
[str2 setString:@"Hello ikilimnik"];
// And list their current values
NSLog(@"str1 = %@, str2 = %@", str1, str2);
//logs as below
//str1 = Hello ikilimnik, str2 = Hello ikilimnik
Notice when you use the immutable string 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 the contents of
the existing Hello Testing
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 is 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.
回答2:
Difference between NSMutableString and NSString:
NSMutableString: NSMutableString objects provide methods to modify the underlying array of characters they represent, while NSString does not. NSMutableString exposes methods such as appendString, deleteCharactersInRange, insertString, replaceOccurencesWithString, etc. All these methods operate on the string as it exists in memory.
NSString: It is a create-once-then-read-only string if you will; you'll find that all of its "manipulation" methods (substring, uppercaseString, etc) return other NSString objects and never actually modify the existing string in memory.
Example :
NSString *simpleString = [[NSString alloc] initWithString:@"simple String"];
simpleString = [simpleString stringByAppendingString:@"OK"];
NSMutableString *mutableString = [[NSMutableString alloc] initWithString:@"MutableString"];
[mutableString appendString:@"OK"];
Both of these, functionally, do the same thing except but - the top code block leaks.
[NSString stringByAppendingString:]
generates a new immutable NSString object which you then tell the pointer to point to. In the process, however, you orphan the NSString object that it originally pointed to.
回答3:
In Objective C, two types of string Objects are there one is mutable and Immutable.
To create an immutable string we use NSString
and to create a mutable string we use NSMutableString
.
If we are creating a string object using NSString
, it is an immutable string object. This means the string cannot subsequently be modified in any way. (although any NSString pointer can be reassigned to a new NSString object.)
NSString *string1 = @"immutable String";
For Mutable strings, we use NSMutableString
, which is a subclass of NSString
.
To assign a mutable string object:
NSMutableString *string2 = [NSMutableString stringWithString:@"Mutable String"];
or
NSMutableString *string2 = [NSMutableString stringWithString: string1]; // string1 is a NSString(immutable)
来源:https://stackoverflow.com/questions/16460779/what-is-actual-difference-between-nsstring-and-nsmutablestring-in-objective-c-wi