Search This Blog

Monday, September 13, 2010

NSArray

//NSArray

//How to create an array and initialize it
NSArray *array = [[NSArray alloc]init];
(or)
NSArray *array = [[NSArray alloc]initWithObjects:@"one",@"two",@"three",nil]; //Here Array is initialized with three String Elements
(or)
NSArray *array2 = [[NSArray alloc]initWithArray:array]; //Here array2 is initialized with the same three elements
(or)
NSArray *array = [[NSArray alloc]initWithContentsOfFile:path]; //Give the path and it will get the Elements

//w/o alloc and init methods
NSArray *array = [NSArray arrayWithObjects:@"one",@"two",@"three",nil];
NSArray *array = [NSArray arrayWithArray:array2];


//If u need only single element in a array
NSArray *array = [NSArray arrayWithObject:@"one"];

//How to get the Count of an Array
int length = [array count];

//How to Add an Element to the array
NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"one",nil];
[array insertObject:@"two" atIndex:1];
[array insertObject:@"three" atIndex:2];

//How to Delete an Element From the Array
[array removeObjectAtIndex:1];

//How to remove Lase Object
[array removeLastObject];


//How to search an element in the Array
if([array containsObject:@"two"])
{
//do this
}

//Displaying the Array Description
NSLog(@"The Array is %@",[array descriptionWithLocale:nil]);
NSLog(@"The Array is %@",[array description]);

//How to get the array element Index
[array indexOfObject:@"two"];

No comments:

Post a Comment