Friday, 6 January 2017

NSArray and NSMutableArray

Hello friends,

In my last post , we have seen NSString and NSMutableString. Today we are going to see a collection type called NSArray and its counter part NSMutableArray.

What is an Array ?

Array is nothing but the collection of similar data type elements.

Lets elaborate our previous statement. What does it actually mean by collection of similar data types?
To clarify this , lets take an example. 

NSArray *arrayOfStrings = [NSArray arrayWithObjects:@"Objective - C",@"Swift 3.0",@"Apple",@"iPhone 7"nil];

Here we are collecting elements of similar data types i.e NSString data type.

Similarly we can create array of other data types such as Int, Float, Double etc. Also we can create array of our custom objects;

In objective c , we have NSArray and NSMutableArray.

1. NSArray:

 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.

Creating an Array 


We can create an array in  several ways. arrayWithObjects is one of them.  For example:
NSArray *fruits;

fruits = [NSArray arrayWithObjects: @"Apple", @"Banana", @"Orange", @"Papaya", nil];
The above code creates a new array object called fruits and initializes it with four constant string objects containing the strings. A nil entry is required to know where the end of the array is located. Failure to include this entry may result in the application crashing, particularly during sort operations.
Because we used the NSArray class in the above example the contents of the array object cannot be changed subsequent to initialization. 

Finding out the Number of Elements in an Array


The number of objects in an array (referred to as elements) can be identified using the count instance method of the NSArray class:
NSArray *fruits;
      
myColors = [NSArray arrayWithObjects: @"Apple", @"Banana", @"Orange", @"Papaya", nil];

NSLog (@"Number of elements in array = %lu", [fruits count]);
When executed, the above code will output the following:
Number of elements in array = 4

Accessing the Elements of an Array 


The array elements are stored based on their index starting from 0, which means our Apple object will be at index 0, Banana object at index 1 and so on.
To access any element from array , we will be referring to this index. Lets do it with  our example:

NSArray *fruits;
int i;
int count;

fruits = [NSArray arrayWithObjects: @"Apple", @"Banana", @"Orange", @"Papaya", nil];
count = [fruits count];

for (i = 0; i < count; i++)
        NSLog (@"Element at index %i is = %@", i, [fruits objectAtIndex: i]);
When we run the code, the output will be:
Element at index 0 is = Apple
Element at index 1 is = Banana
Element at index 2 is = Orange
Element at index 3 is = Papaya

Accessing Array Elements using Fast Enumeration


The technique for accessing all the array elements using a for loop as described in the previous section is a little ungainly. Another, easier mechanism for accessing element in an array involves something called fast enumeration. Fast enumeration simply requires that a variable be declared to hold each array element, and then referenced in the for loop:
NSArray *fruits = [NSArray arrayWithObjects: @"Apple", @"Banana", @"Orange", @"Papaya", nil];

for (NSString *fruit in fruits)
        NSLog (@"Fruit %d = %@",i+1, color);


Those were few important methods regarding NSArray. You can find full list of NSArray APIs at Apple Documentation.

1. NSMutableArray:

 Unlike NSArray, NSMutableArray lets you change its contents i.e , after creating your array initially, if you want to modify it at any time in the project , you can do that.

Adding Elements to an Array


New elements may be added to a mutable array object using the addObject instance method of the NSMutableArray class. For example, to declare and initialize an array, and then later add new element object the following code might be used:
NSMutableArray *fruits = [NSMutableArray arrayWithObjects: @"Apple", @"Banana", nil];

[fruits addObject: @"Orange"];
[fruits addObject: @"Papaya"];

Inserting Elements into an Array


The previous method appends new objects onto the end of an array. It is also possible to insert new objects at specific index points in an array object using the insertObject instance method:

;
int i;
int count;

NSMutableArray *fruits = [NSMutableArray arrayWithObjects: @"Apple", @"Banana", @"Orange", @"Papaya", nil];

[fruits insertObject: @"Grapes" atIndex: 1];
[fruits insertObject: @"Gauva" atIndex: 3];

count = [fruits count]; 

for (i = 0; i < count; i++)
        NSLog (@"Fruit %i = %@", i, [fruits objectAtIndex: i]);
When we compile and run the code, the following output confirms that the new objects were indeed inserted at the specified index positions:
Fruit 0 = Apple
Fruit 1 = Grapes
Fruit 2 = Banana
Fruit 3 = Gauva
Fruit 4 = Orange
Fruit 5 = Papaya

Deleting Elements from an Array 


To remove an element at a specific index location, use the removeObjectAtIndex method:
NSMutableArray *fruits = [NSMutableArray arrayWithObjects: @"Apple", @"Banana", @"Orange", @"Papaya", nil];

[fruits removeObjectAtIndex: 0];
To remove the first instance of a specific object from an array use removeObject:
NSMutableArray *fruits = [NSMutableArray arrayWithObjects: @"Apple", @"Banana", @"Orange", @"Papaya", nil];

[fruits removeObject: @"Orange"];
To remove all instances of a specific object in an array, use removeObjectIdenticalTo:
NSMutableArray *fruits = [NSMutableArray arrayWithObjects: @"Apple", @"Banana", @"Orange", @"Papaya", @"Apple", @"Apple", nil];

[fruits removeObjectIdenticalTo: @"Apple"];
To remove all objects from an array, use removeAllObjects:
NSMutableArray *fruits = [NSMutableArray arrayWithObjects: @"Apple", @"Banana", @"Orange", @"Papaya", nil];

[fruits removeAllObjects];
To remove the last object in the array, use the removeLastObject method:
NSMutableArray *fruits = [NSMutableArray arrayWithObjects: @"Apple", @"Banana", @"Orange", @"Papaya", nil];

[fruits removeLastObject];

Sorting Array Objects


The Foundation Framework NSArray class, and the subclasses thereof, provide a number of mechanisms for sorting the elements of an array into a specific order. The simplest way to achieve this is to use the sortedArrayUsingSelector instance method. For example, to perform a sort on our example array using this method, we could use the following code:
NSMutableArray *fruits = [NSMutableArray arrayWithObjects: @"Apple", @"Banana", @"Orange", @"Papaya", nil];

NSArray *sortedArray;

sortedArray = [myColors sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
As we can see from the above example, the method returns a new array containing the elements of the original array sorted using the localizedCaseInsensitiveCompare method. 

More about NSMUtableArray can be found at Apple Documentation

I hope you guys would have got an idea about dealing with collection of elements and operating over them.

In the next post we are going to see another collection data type - NSDictionary and NSMutableDictionary, till then.......




Share:

0 comments:

Post a Comment