Monday, September 10, 2012

iphone Objective C Array in iphone exam

Objective C Array

In a simple term we can say that array is a object, which is a collection of number of other objects. In Objective C, we can also add and remove the objects from the given list of array if the array is declared mutable.
Syntax to declare an Array in Objective C:
NSArray *myArray
or
NSMutableArray *myMutableArray
You can declare an array either in header file (.h) or in implementaion file (.m) at the time of implementation.
Declaring an Array at the time of implementation
NSArray *myArray;
myArray = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
or Mutable Array like:
NSMutableArray *myMutableArray;
myMutableArray = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
We can also add or insert more objects in an array even after declaring it if it is a mutable array… for example
"addObject" in an array
NSMutableArray *myMutableArray;
myMutableArray = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
[myMutableArray addObject:@"Orange"];
[myMutableArray addObject:@"Black"];
"insertObject" in an array
NSMutableArray *myMutableArray;
myMutableArray = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
[myMutableArray insertObject:@"Orange" atIndex: 2];
[myMutableArray insertObject:@"Black" atIndex: 5];

No comments:

Post a Comment