Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSMutableArray vs NSArray which is better

Tags:

objective-c

This is a bit of a silly question, but if I want to add an object to an array I can do it with both NSMutableArray and NSArray, which should I use?

NSMutableArray * array1;
[array1 addObject:obj];
NSArray * array2;
array2 = [array2 arrayByAddingObject:obj];
like image 617
Jordan Medlock Avatar asked Nov 21 '11 04:11

Jordan Medlock


1 Answers

Use NSMutableArray, that is what it is there for. If I was looking at code and I saw NSArray I would expect it's collection to stay constant forever, whereas if I see NSMuteableArray I know that the collection is destined to change.

It might not sound like much right now, but as your project grows and as you spend more time on it you will see the value of this eventually.

like image 99
sosborn Avatar answered Oct 12 '22 13:10

sosborn