Difference between NSArray and NSMutableArray
44,215
Solution 1
NSMutableArray
(and all other classes with Mutable
in the name) can be modified. So, if you create a plain NSArray
, you cannot change its contents later (without recreating it). But if you create an NSMutableArray
, you can change it — you'll notice it has methods like -addObject:
and -insertObject:atIndex:
.
See the documentation for details.
Solution 2
The "mutable" types are classes which can be changed after they've been initialized, like NSMutableString
vs NSString
.
Solution 3
NSArray : in NSArray we can not change index.... Means fix array.
NSMutableArray : in NSMutableArray we can change index and also add the value in array at run-time..
Comments
-
g.revolution over 4 years
What is the difference b/w
NSArray
andNSMutableArray
? -
mmx over 14 years
NSMutableString
is derived fromNSString
. Consequently, you can't rely on a "NSString*
" you receive from outside to be immutable. You can only assumeNSMutableString*
is mutable.NSString*
can be mutable or immutable. That's why you might want to call[str copy]
when the instance is assigned to some property in your class. -
Shaggy Frog over 14 yearsYou can't change an NSString once it's built. See stackoverflow.com/questions/905396/…. As for why you use copy, see stackoverflow.com/questions/387959/…
-
mmx over 14 yearsShaggy: You can't change an instance of
NSString
class. This is not the issue. The issue is that a "NSString*
" does not necessarily point to an instance ofNSString
class. It can also point to instances of classes derived fromNSString
, likeNSMutableString
. Therefore, you can't rely on anNSString*
you have received from the outside world to be immutable. -
Shaggy Frog over 14 yearsOkay, I understand now. I'll delete that portion of my answer.