Skip to content

November 10, 2014

Play With Arrays in Objective C

by noise

Next example show you how to create an array in objective C, how to iterate through the array and how to add or remove elements from the array.

NSArray *anArray = @[@"element1", @"element2", @"element3"];
// iterate through the array
for (NSString *aString in anArray) {
    NSLog(@"%@", aString);
// adding elements to an array
NSMutableArray *anotherArray = [[NSMutableArray alloc] init];
[anotherArray addObject:@"element10"];
[anotherArray addObject:@"element11"];
[anotherArray addObject:@"element12"];
for (NSString *aString in anotherArray) {
    NSLog(@"%@", aString);
// insert object at a specified position
[anotherArray insertObject:@"elem22" atIndex:1];
for (NSString *aString in anotherArray) {
    NSLog(@"%@", aString);
// remove element from a specified position
[anotherArray removeObjectAtIndex:3];
// remove last object
[anotherArray removeLastObject];
// find number of records in a NSArray or NSMutableArray
NSInteger noOfRows = (NSInteger)[anotherArray count];
NSLog(@"Number of rows in the array %ld", noOfRows);
Read more from Objective C Tips

Leave a Reply


Note: HTML is allowed. Your email address will never be published.

Subscribe to comments