iPhone : Exploring the Foundation Framework – NS Array Class


NSArray is Objective-C’s general-purpose array type. It represents an ordered collection of objects, and it provides a high-level interface for sorting and otherwise manipulating lists of data. (Refer to Exploring the Foundation Framework, to know about types of Foundation Classes).

NSArray and its subclass NSMutableArray manage ordered collections of objects called arrays. NSArray creates static arrays, and NSMutableArray creates dynamic arrays. You can use arrays when you need an ordered collection of objects.

NSArray is “toll-free bridged” with its Core Foundation counterpart, CFArrayRef.

  NSArray  *brray  =[[NSArray alloc]init];

//  Initialization:

NSArray*aArray  =[[NSArray alloc]
                  initWithObjects:@ “one”,@ “two”,@ “three”, nil];
NSLog (@ “the array is %@”, aArray);

//Using Class method:

NSArray *aArray =[NSArray arraywithObjects :@ “one, @two”, nil];
NSLog(@ “the array is %@”, aArray);

//count of an array:

Int Count  =  [a Array Count];

// printing array:

NSLog (@ “array is %@”, aArray);

// How to get array objects:

NSString *Second Element = [aArray ObjectAtIndex:1];
Ns log (@ “Second Element is %@, Second Element);

// How to add objects to an array:

 [[NSMutableArray *a mutArray  =
 [[NSMutableArray alloc] init with objects:@ “hai”,@ “hello”, nil];
[a mutArray addObjects : a Array ];
NSLog(@ “a mutArray is %@”, a mutArray);

// output of an a mutArray is

/ *(

//How to access two in the above array:

NSArray *temporary = [a mutArray objectAtIndex:2];
NSString *reqElement = [ tempArray objectAtIndex :1];
NSLog (@ “req.element is %@” , req Element);

// Remove object at particular index:

  [a mutArray removeObjects AtIndex :1 ];
NSLog (@  “ the array after removal is %@”, a mutArray);

Remove all objects:

[a mutArray removeAllObjects];
NSLog (@  “the array remove all objects];
NSLog (@ “ the array after removal is %@”, a mutArray);

// Insertion of object at particular Index:

[a mutArray insertObject :@  “good”  atIndex:1];
NSLog (@ “the insertion of object is %@’, a mutArray);

// getting last object of an object:

[a mutArray LastObject];

removing LastObject:

[a mutArray  remove last object];

// element search in an Array:

 aArray  =  one, two, three, four, five……
NSString   *Str  =   @  “five;
For  (int  i=0,  i<[aArray   Count];i++)
If([aArray objectAtIndex :i]= = str)
      // array have five.
BooL value = [aArray ContainsObjects :Str];
If (value)

0 Responses on iPhone : Exploring the Foundation Framework – NS Array Class"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.