Home Tutorial Iphone Examples Objective C Array

 
 

Objective C Array
Posted on: October 6, 2010 at 12:00 AM
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.

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];

and so on…

Related Tags for Objective C Array:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.