Difference between NSString and NSMutableString

NSString and NSMutableString demo app

Difference between NSString and NSMutableString  is a common interview question faced by every programmer and the most familiar answer to this is that one can append string in NSMutableString and not in NSString. So question arrived from other end,  any other difference you know?. As I can append string to NSString too. In this post I am going to tell you the main difference between NSString and NSMutableString.

                            To know the exact answer or difference let us start with few examples.

 First take a variable/object of NSString named simpleString1 and assigned it a value NSString1. Next take another variable/object of NSString named simpleString2 and assigned it simpleString1. Print both of string objects in NSLog. Now assign new value to simpleString2, let’s say we assign a value
NSString2Changed. Again Print both string objects in NSLog.

    NSString * simpleString1 = @”NSString1″;
    NSString * simpleString2 = simpleString1;
    NSLog(@”simpleString1 = %@, simpleString2 = %@”, simpleString1, simpleString2);

    simpleString2 = @”NSString2Changed”;
    NSLog(@”simpleString1 = %@, simpleString2 = %@”, simpleString1, simpleString2);

Simple NSString code

As we are done with NSString, now its time for NSMutableString to come into action. Similarly as done before take two objects/variables of NSMutableString named mutableString1 and mutableString2. Assign value NSMutableString1 to mutableString1 object and assign mutableString1 to mutableString2. Print both mutable objects in NSLog. Now assign or change value of mutableString2 to NSMUtableStringChanged2. Print both objects again in NSLog.

   NSMutableString * mutableString1 = [NSMutableString stringWithString:@”NSMutableString1″];
    NSMutableString * mutableString2 = mutableString1;
    NSLog(@”mutableString1 = %@, mutableString2 = %@”, mutableString1, mutableString2);
   
    [mutableString2 setString:@”NSMUtableStringChanged2″];
    NSLog(@”mutableString1 = %@, mutableString2 = %@”, mutableString1, mutableString2);

NSMutableString Code

Now when you clearly look at the output printed in console you can see that in case of NSString value remain unchanged for  simpleString1 when we assign a different value to simpleString2. But in case of NSMutableString its changed. This is because  mutable objects always point to same memory position when they are directly assigned form another NSMutableString whereas  immutable objects not.

Below is the output of console

If you alloc mutableString2 instance of  NSMutableString and then assign it mutableString1, the result would be differ.

 NSMutableString * mutableString1 = [NSMutableString stringWithString:@”NSMutableString1″];
 NSMutableString * mutableString2 = [[NSMutableString alloc]initWithString:mutableString1];
 NSLog(@”mutableString1 = %@, mutableString2 = %@”, mutableString1, mutableString2);


 [mutableString2 setString:@”NSMUtableStringChanged2″];
 NSLog(@”mutableString1 = %@, mutableString2 = %@”, mutableString1, mutableString2);

Source Code: Demo Code