What is the difference between NSArray and NSMutableArray?

Regular NSArray is Immutable , i,e  whenever you create an NSArray object and initialize some values to that and later sometime in the project if u have to add some value or object to that instance of an array it won't let u do that.

NSArray *myArray = [NSArray arrayWithObjects: @"1", @"two", @"3", nil];

Here unlike the C-style Arrays you don't explicitly declare the size of an array, it will automatically does it until it hits nil.
The problem here is, sometime later if u want to add or remove object , you will not be able to do with the regular arrays. NSMutableArray which is the subclass of NSArray does this for you.

NSMutableArray *myArray = [NSArray arrayWithObjects: @"1", @"two", @"3", nil];

sometime later  if you want to add a string or object to this array, there are methods in NSMutableArray which lets you to do this.

[myArray addObject: @"four"];
[myArray removeObjectAtIndex:2];

6 comments:

  1. Nice one ... it helps me to understand the difference.

    Thank you.

    ReplyDelete
  2. HI am beginner and Am working As a Ios Developer in Level 2company. Will thr be much scope in future and wat all are companies wil hav Ios application.. can u suggest me

    ReplyDelete
  3. You could just work harder and write an original answer instead of copying it verbatim from Stackoverflow, right?

    ReplyDelete
  4. please explain me atomic and nonatomic with example

    ReplyDelete
    Replies
    1. https://www.quora.com/What-is-the-difference-between-strong-retain-nonatomic-etc-in-the-Objective-C-iOS-property

      Delete