Wednesday, 25 March 2015

NSArray-Indepth Understanding and Usability

Objective:
               To get a deeper understanding of NSArray and it's usabilities.


               These are the list of things we are about to achieve:
                1.To find the object at index;
                2.To iterate array using FastEnumeration and iterate over normal For Loop;
                3.To find if two array's are equal or not;
                4.To Enumerate array using blocks;
                5.Membership Checking-Bool Checking and Index Checking;
                6.Sort Array;
                7.SubDividing Arrays;
                8.Combine Arrays;
                9.Adding String Inbetween array objects.

What is an NsArray:
           NsArray along with NSMutable Array is an ordered collection of objects in objective c.NSArray creates a static,immutable array. ie.The values inside the NSArray cannot be changed.

Solution:
1.To create an array and to find the object at index:

OUTPUT:




 2.To iterate array using FastEnumeration and iterate over normal For Loop:

Fast enumeration is the most efficient way to iterate over NSArray and it's content are guaranteed to appear in the correct order.


OUTPUT:





The same can be done using traditional for loop:





OUTPUT:




3.To find if two array's are equal or not:

   Method Used:isEqualToArray.









OUTPUT:


4.To enumerate NSArray using blocks

Method Used:EnumerateObjectsUsingBlocks;

Signature:(id obj,NSUInteger idx,BOOL *stop)




OUTPUT:





The objects are passed in the same order as they appear in the NSArray.

5.Membership Checking-Bool Checking and Index Checking:

1.BOOL Checking:

Method Name:ContainsObject;

It returns YES if the object is in the array else it will return NO


OUTPUT:




2.Index Checking:

Method Used:indexOfObject;

This will either return the requested index of object or will return NSNotFound;
OUTPUT:



6.Sort Array:

Sorting is one of the main advantage of using NSArray.

Method Used:SortedArrayUsingComparator.

This accepts an NSComparsionResult(id obj1,id obj2)block,which should one of the following enumerators depending upon the relationship between obj1 & obj2.


Return ValueDescription
NSOrderedAscendingobj1 comes before obj2
NSOrderedSameobj1 and obj2 have no order
NSOrderedDescendingobj1 comes after obj2
OUTPUT:



7.SubDividing Array:

Method Used:SubArrayWithRange.


OUTPUT:



8.Combining Arrays:

Method Name:arrayByaddingObjectsFromArray;

It takes two arrays and combines the two arrays using the above method.this returns as a new array containing all the elements of the original array,along with the contents of original parameters.


OUTPUT:



9.Adding a string element in-between array object:

Method Used:ComponentsJoinedByString;

The above element concatenates each element of the array into a string,separated by the specified  symbols.

OUTPUT:

No comments:

Post a Comment