NSArray and NSMutableArray in Objective-C
Last Updated :
13 Dec, 2022
An array is a data structure that allows to store data in contiguous memory. They can be used to store primitive data types such as int, float, double, char, etc of any particular type or can store derived data types such as structures, pointers, etc. Its indexing starts with 0 i.e. the first element is stored at the 0 index, the second element at the 1 index, and so on. It can be mutable and immutable.
Mutability means changeable i.e. if something is mutable we can change that thing. In an array or string, mutability means that we can change an array element or a string’s character can be changed during runtime. But if an array or string is immutable, we can’t change an array element or a string’s character during runtime.
NSArray
In an Objective-C programming language, NSArray is used instead of an array to store data values in contiguous memory. The indexing in NS Array remains the same as that of an array. NSArray in Objective C stores an immutable array of objects. Since it is immutable, we can’t change array elements during runtime, but we only replace the existing array. It manages ordered collections of objects. It is used to create a static collection of objects i.e. static array.
Some methods of NS Array
- alloc/initWithObjects: It is used to initialize an array with objects.
- objectAtIndex: It returns the object at the index given.
- count: It returns the count of objects in the array.
- containsObject: It returns true if the given object is in the array otherwise returns false.
- indexOfObject: It returns the index at which the object is stored in the array.
Example:
In the below given example, we are initializing the NSArray named array with three string objects. Then we are applying the above-given methods to find objects at the index, count the number of objects, check whether an object is present or not, etc.
ObjectiveC
#import <Foundation/Foundation.h>
int main()
{
NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];
NSArray *array = [[ NSArray alloc] initWithObjects: @"geeks" , @"for" , @"gfg" , nil ];
NSLog ( @"The object in array at Index 1 is : %@ \n" , [array objectAtIndex:1]);
NSLog ( @"Total number of objects in the array is : %d \n" , [array count]);
BOOL containsObject = [array containsObject: @"gfg" ];
if (containsObject == true )
NSLog ( @"Yes\n" );
else
NSLog ( @"false\n" );
int index = [array indexOfObject: @"gfg" ];
NSLog ( @"gfg is at index : %d \n" , index);
[pool drain];
return 0;
}
|
Output
The object in array at Index 1 is : for
Total number of objects in the array is : 3
Yes
gfg is at index : 2
NSMutable Array
In Objective-C programming language, NSMutable Array is also used instead of an array or NSArray to store data values in contiguous memory. The indexing in an NSMutable Array remains the same as that of an array or an NSArray. The difference between NSArray and NSMutable Array in Objective C is the mutability of the NSMutable Array. It stores a mutable array of objects. Since it is mutable, we can change array elements during runtime. It also manages ordered collections of objects. NSMutable Array creates a dynamic collection of objects i.e dynamic arrays.
Some methods of NS Mutable Array
Since the NSMutable array is inherited from NSArray, all the instance methods of the NSArray can be used in the NSMutable Array. In addition to these methods, we also use the below-mentioned methods.
- addObject: It is used to insert an object at the end of the array.
- insertObject: atIndex: It is used to insert an object at the given index.
- removeObjectAtIndex: It is used to remove an object from the array at specified index.
- removeLastObject: It is used to remove the last object from the array.
- replaceObjectAtIndex: withObject: It replaces an object at a specified index with given object.
- removeAllObjects: It deletes all the elements of the array, hence making it empty.
Example:
In the below given example, we are initializing an empty NS Mutable Array named mutableArray. Then we are applying the above-given methods to add objects, print, add objects at the required index, remove objects from the given index, remove the last object, replace, etc.
ObjectiveC
#import <Foundation/Foundation.h>
int main()
{
NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];
NSMutableArray *mutableArray = [[ NSMutableArray alloc]init];
[mutableArray addObject: @"geeks" ];
[mutableArray addObject: @"for" ];
[mutableArray addObject: @"write" ];
[mutableArray addObject: @"intern" ];
NSLog ( @"mutableArray : %@ \n" ,mutableArray);
[mutableArray insertObject: @"gfgwebsite" atIndex:1];
NSLog ( @"mutableArray after insertion : %@ \n" , mutableArray);
[mutableArray removeObjectAtIndex:1];
NSLog ( @"mutableArray after removing : %@ \n" , mutableArray);
[mutableArray removeLastObject];
NSLog ( @"mutableArray after removing last object : %@ \n" , mutableArray);
[mutableArray replaceObjectAtIndex:0 withObject: @"swift" ];
NSLog ( @"mutableArray after replacing 0th index object : %@ \n" , mutableArray);
NSLog ( @"No of objects before removing all objects : %d \n" , [mutableArray count]);
[mutableArray removeAllObjects];
NSLog ( @"No of objects before removing all objects : %d \n" , [mutableArray count]);
[pool drain];
return 0;
}
|
Output
mutableArray : (geeks, for, write, intern)
mutableArray after insertion : (geeks, gfgwebsite, for, write, intern)
mutableArray after removing : (geeks, for, write, intern)
mutableArray after removing last object : (geeks, for, write)
mutableArray after replacing 0th index object : (swift, for, write)
No of objects before removing all objects : 3
No of objects before removing all objects : 0
Share your thoughts in the comments
Please Login to comment...