what is difference mutable and immutable
like
NSString and NSMutableString.
NSArray and NSMutableArray.
NSDictionary and NSMutableDictionary
Mutable objects can be modified, immutable objects can't.
Eg:
NSMutableArray has addObject:
removeObject:
methods (and more), but NSArray doesn't.
Modifying strings:
NSString *myString = @"hello";
myString = [myString stringByAppendingString:@" world"];
vs
NSMutableString *myString = @"hello";
[myString appendString:@" world"];
Mutable objects are particularly useful when dealing with arrays,
Eg if you have an NSArray of NSMutableStrings you can do:
[myArray makeObjectsPerformSelector:@selector(appendString:) withObject:@"!!!"];
which will add 3 ! to the end of each string in the array.
But if you have an NSArray of NSStrings (therefore immutable), you can't do this (at least it's a lot harder, and more code, than using NSMutableString)
The basic difference is:
NSStrings cannot be edited, only reassigned. This means when the value of an NSString changes, it is actually pointing to a new location in memory.
NSMutableString objects can be edited and maintain the same pointer.
A common practical difference is:
If you create 1 NSString and then assign another one to it, then edit either one of them, they will now be pointing to different strings.
If you do the same thing with NSMutableStrings, but then just edit one of them (not reassign it), they will both be pointing to the newly edited object.