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:
- count: This method is used to determine the array count.
- 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:
- insertObject:(id)anObject atIndex:(NSUInteger)index: Inserts the given object in the array’s content at the given index.
- removeObjectAtIndex:(NSUInteger)index: Removes the object from the given index.
- replaceObjectAtIndex:(NSUInteger)index WithObject:(id)Object: This method is used to replace the object at the given index with the new object.
- 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
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 email@example.com until then happy iCoding and have a great Day.