-1

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

Sapana Ranipa
  • 889
  • 7
  • 20
user1960279
  • 494
  • 1
  • 8
  • 24
  • 3
    possible duplicate of [the documentation](http:///developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableString_Class/Reference/Reference.html) – Stephen Darlington May 09 '13 at 12:13

2 Answers2

15

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.

Community
  • 1
  • 1
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
  • For the immutable: why doesn't str2 become immutable right after str2=str1? How is it that it can still get a new value of "Hello hehehe"? – mfaani Mar 12 '16 at 11:56
-1

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)
ocodo
  • 29,401
  • 18
  • 105
  • 117
Rajesh
  • 7
  • 1
  • Downvoted: Tried editing this for you to clarify the language, but I'd suggest talking about the use-case for immutable vs mutable strings. That seems to be the way to uncover the _actual_ differences, instead of merely the _syntactic_ initialization differences. The accepted answer does a good job. Better luck next time! – ocodo Mar 23 '18 at 10:27