1

With NSMutableArray we get additional methods like

  • addObject
  • insertObject
  • removeObjectAtIndex

Why we want to use non-mutable NSArray when we get additional methods in NSMutableArray? Is there any performance penalty when using NSMutableArray over NSArray?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
jay_x86
  • 21
  • 3
  • 1
    Memory is quite a good reason. Block any attempts of modification is a good one too. – Larme May 23 '15 at 10:53
  • Why would you use a mutable array for things like storing the letters of the alphabet? Or something like that? – Fogmeister May 23 '15 at 10:58

1 Answers1

0

Just check this link:

It is mentioned in the above link, just portraying in my words:

While calling an API that wants to copy the array and if you send -copy to an immutable array(NSArray), it just bumps the retain count, but while sending -copy to a mutable array(NSMutableArray) will allocate heap memory. So bumping the retain count is much faster than allocating heap memory.

Here comes the performance issue.

Community
  • 1
  • 1
Vizllx
  • 9,135
  • 1
  • 41
  • 79