RSS

NSArray and NSMutableArray

24 Aug

NSArray Class:  In objective c there are two classes that are used for arrays one is NSArray and the other is NSMutableArray now the question that comes into ones mind is when to use NSArray and when to use NSMutableArray well the answer is quite simple. You will use NSArray when you are sure that the data that you will be storing inside the NSArray will be static and not dynamic. Both the classes have two methods that are used more by developers in their day to day activity and they are:

  1. count: This method is used to determine the array count.
  2. objectAtIndex:(NSUInteger)index: This methods returns the object or value located at the particular index of the array. This method accepts an integer value this integer value is actually the index number of the array whose data you want to display.

Let’s have a look at a simple example of this class where we will insert string objects into the array and just display them using NSLog function.

Here’s a view at the the .h part of the file

Code Explanation: In the above class I have declared the object of the class NSArray also I have declared an instance method named displayArray with the help of which we shall display the contents inside the array.

The code present inside the MyArrayClass.m i.e the implementation file

Code Explanation: In the above code I am initializing the array inside the init method as you can see from the above code that the array is initialized with the string objects and has a nil after the last element of the array element which indicates this is the last element of the array this concept is similar to link list where in the last pointer in the structure points towards nil. It is very compulsory that you write nil at the end of every last element of the array whether its NSArray or NSMutableArray. The code inside the displayArray must be very simple to you as I have used a for loop to display the contents inside the array with the help of the method named objectAtIndex.

NSMutableArray: NSMutableArray class extends the normal array handling behavior of array to modify the contents present inside the array (insert, delete, modify). This class is inherited from NSarray class commonly used NSMutableArray methods are given below:

  1. insertObject:(id)anObject atIndex:(NSUInteger)index: Inserts the given object in the array’s content at the given index.
  2. removeObjectAtIndex:(NSUInteger)index: Removes the object from the given index.
  3. replaceObjectAtIndex:(NSUInteger)index WithObject:(id)Object: This method is used to replace the object at the given index with the new object.
  4. addObject: (id)anObject: Inserts a new object at the end of the mutable array.

Let’s have a look at the example of NSMutableArray

The code inside the MyArrayClass.m file is given below

Code Explanation: As you can see from the above code that I have just used the methods that were previously explained to you and you can see from the above implementation that how easy it is to use arrays in objective c. In the above code there is a piece of line which says this

[self displayMutabelArray];

In Objective C self is like the this pointer that you have in C++ which points towards the current class, so what does the above expression says that if there is a method called as displayMutableArray inside the current class (in this case it’s the MyArrayClass) then call that method.

I hope you have understood the difference between NSArray and NSMutableArray a copy of the entire post is present as a PDF file here, Kindly let me know if you are having any sort of questions or queries via comments or you can mail me directly at iphone.learning@quagnitia.com until then happy iCoding and have a great Day.

Advertisements
 
2 Comments

Posted by on August 24, 2011 in Beginners

 

Tags: , , , ,

2 responses to “NSArray and NSMutableArray

  1. Vinod Shimpi

    August 25, 2011 at 6:09 am

    Array is Very Essential part in any Programming Language.
    So we get details Knowledge about the array from this Notes.
    And the Code was very simple to Understand.

     
  2. Milind Mahajan

    August 25, 2011 at 6:38 am

    Superlike… I was facin sm dificulty bt dis documnt helpd me a lot… Request u 2 write on othr topics in iPhone early as possible…

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: