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];

11 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
  5. Excellent…Amazing…. I’m satisfied to find so many helpful information here within the put up,for latest php jobs in near me. we want work out extra strategies in this regard, thanks for sharing.

    ReplyDelete
  6. Top SEO Company The best known mobile App design and development company based in Lahore Pakistan. Get applications for Android, iOS, HTML5, Windows.
    IOS Apps Development in Lahore

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete