Sep 8, 2013

Objective-C Arrays

As far as Objective-C Arrays go, let’s simply jump to the examples instead of starting with the usual boring definition and other stuff. In this post you will see examples related to NSArray, NSMutableArray, addObject, insertObject, removeLastObject, removeObjectAtIndex, removeAllObjects, sortedArrayUsingSelector.

Example 1: A simple example of NSArray. Printing elements of an array.

#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSArray *myShirts;
NSString *shirts;
int count;
myShirts = [NSArray arrayWithObjects: @"CK", @"AF", @"GUCCI", @"ARMANI", nil];
NSLog (@"Number of elements in array = %lu", [myShirts count]);
for (shirts in myShirts)
NSLog (@"Element %@", shirts);
return 0;
}

Output:
Number of elements in array = 4
Element CK
Element AF
Element GUCCI
Element ARMANI

arrayWithObject returns an auto-released array so you do not have to worry about releasing it when you don’t need it anymore (but if you store it in an instance variable, you should retain it to prevent the auto release pool from freeing it). initWithObject returns an array with a retain count of 1, i.e. you own the array and you must release it at some point to prevent memory leaks. [Source]

Example 2: Printing elements as well as element number of an array. Using objectAtIndex.

#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSArray *myShirts;
NSString *shirts;
int i;
int count;
myShirts = [NSArray arrayWithObjects: @"CK", @"AF", @"GUCCI", @"ARMANI", nil];
NSLog (@"Number of elements in array = %lu", [myShirts count]);
count = [myShirts count];
for (i = 0; i < count; i++)
NSLog (@"Element %i = %@", i, [myShirts objectAtIndex: i]);
return 0;
}

Output:
Number of elements in array = 4
Element 0 = CK
Element 1 = AF
Element 2 = GUCCI
Element 3 = ARMANI

NSMutableArray is editable i.e. its contents can be modified, where as NSArray is read-only i.e. you cannot change the contents of NSArray, but you can recreate it.

Lets now create an NSMutableArray and learn other methods like addObject etc.

Example 3: Adding elements into an array and printing them. Using addObject.

#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *myShirts;
NSString *shirts;
int i;
int count;
myShirts = [NSMutableArray arrayWithObjects:
@"CK",@"AF",@"GUCCI",@"ARMANI",nil];
NSLog (@"Number of elements in array = %lu", [myShirts count]);
[myShirts addObject: @"French Connection"];
[myShirts addObject: @"Gas"];
NSLog (@"Number of elements in array = %lu", [myShirts count]);
count = [myShirts count];
for (i = 0; i < count; i++)
NSLog (@"Element %i = %@", i, [myShirts objectAtIndex: i]);
return 0;
}

Output:
Number of elements in array = 4
Number of elements in array = 6
Element 0 = CK
Element 1 = AF
Element 2 = GUCCI
Element 3 = ARMANI
Element 4 = French Connection
Element 5 = Gas

Addobject appends at the end. With insertObject you can insert at a specific location.

Example 4: Adding elements into an array and printing them. Using insertObject.

#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *myShirts;
NSString *shirts;
int i;
int count;
myShirts = [NSMutableArray arrayWithObjects: @"CK", @"AF", @"GUCCI",
@"ARMANI", nil];
NSLog (@"Number of elements in array = %lu", [myShirts count]);
count = [myShirts count];
for (i = 0; i < count; i++)
NSLog (@"Element %i = %@", i, [myShirts objectAtIndex: i]);
[myShirts insertObject: @"French Connection" atIndex: 0];
[myShirts insertObject: @"Gas" atIndex: 1];
NSLog (@"\n");
count = [myShirts count];
NSLog (@"Number of elements in array = %lu", [myShirts count]);
count = [myShirts count];
for (i = 0; i < count; i++)
NSLog (@"Element %i = %@", i, [myShirts objectAtIndex: i]);
return 0;
}

Output:
Number of elements in array = 4
Element 0 = CK
Element 1 = AF
Element 2 = GUCCI
Element 3 = ARMANI

Number of elements in array = 6
Element 0 = French Connection
Element 1 = Gas
Element 2 = CK
Element 3 = AF
Element 4 = GUCCI
Element 5 = ARMANI

Example 5: Removing last element in an array using removeLastObject.

#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *myShirts;
NSString *shirts;
int i;
int count;
myShirts = [NSMutableArray arrayWithObjects: @"CK", @"AF", @"GUCCI", 
@"ARMANI", nil];
NSLog (@"Number of elements in array = %lu", [myShirts count]);
count = [myShirts count];
for (i = 0; i < count; i++)
NSLog (@"Element %i = %@", i, [myShirts objectAtIndex: i]);
NSLog (@"\n");
[myShirts removeLastObject];
NSLog (@"Number of elements in array = %lu", [myShirts count]);
count = [myShirts count];
for (i = 0; i < count; i++)
NSLog (@"Element %i = %@", i, [myShirts objectAtIndex: i]);
return 0;
}

Output:
Number of elements in array = 4
Element 0 = CK
Element 1 = AF
Element 2 = GUCCI
Element 3 = ARMANI
Number of elements in array = 3
Element 0 = CK
Element 1 = AF
Element 2 = GUCCI

Example 6: Removing an element from an array using removeObjectAtIndex.

#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *myShirts;
NSString *shirts;
int i;
int count;
myShirts = [NSMutableArray arrayWithObjects: @"CK", @"AF", 
@"GUCCI", @"ARMANI", nil];
NSLog (@"Number of elements in array = %lu", [myShirts count]);
count = [myShirts count];
for (i = 0; i < count; i++)
NSLog (@"Element %i = %@", i, [myShirts objectAtIndex: i]);
NSLog (@"\n");
[myShirts removeObjectAtIndex: 3];
NSLog (@"Number of elements in array = %lu", [myShirts count]);
count = [myShirts count];
for (i = 0; i < count; i++)
NSLog (@"Element %i = %@", i, [myShirts objectAtIndex: i]);
return 0;
}

Output:
Number of elements in array = 4
Element 0 = CK
Element 1 = AF
Element 2 = GUCCI
Element 3 = ARMANI

Number of elements in array = 3
Element 0 = CK
Element 1 = AF
Element 2 = GUCCI

Example 7: Removing all elements from an array using removeAllObjects.

#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *myShirts;
NSString *shirts;
int i;
int count;
myShirts = [NSMutableArray arrayWithObjects: @"CK", @"AF", 
@"GUCCI", @"ARMANI", nil];
NSLog (@"Number of elements in array = %lu", [myShirts count]);
count = [myShirts count];
for (i = 0; i < count; i++)
NSLog (@"Element %i = %@", i, [myShirts objectAtIndex: i]);
NSLog (@"\n");
[myShirts removeAllObjects];
//at this point array element count is 0.
if (!myShirts || !myShirts.count){
NSLog (@"No Shirts!");
}
return 0;
}

Output:
Number of elements in array = 4
Element 0 = CK
Element 1 = AF
Element 2 = GUCCI
Element 3 = ARMANI

No Shirts!

Example 8: Removing all instances of a particular object from an array using removeObject.

removeObject removes the all instances of an object in an array.

#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *myShirts;
NSString *shirts;
int i;
int count;
myShirts = [NSMutableArray arrayWithObjects: @"CK", @"AF", 
@"GUCCI", @"ARMANI", @"CK", nil];
NSLog (@"Number of elements in array = %lu", [myShirts count]);
count = [myShirts count];
for (i = 0; i < count; i++)
NSLog (@"Element %i = %@", i, [myShirts objectAtIndex: i]);
NSLog (@"\n");
[myShirts removeObject: @"CK"];
NSLog (@"Number of elements in array = %lu", [myShirts count]);
count = [myShirts count];
for (i = 0; i < count; i++)
NSLog (@"Element %i = %@", i, [myShirts objectAtIndex: i]);
return 0;
}

Output:
Number of elements in array = 5
Element 0 = CK
Element 1 = AF
Element 2 = GUCCI
Element 3 = ARMANI
Element 4 = CK

Number of elements in array = 3
Element 0 = AF
Element 1 = GUCCI
Element 2 = ARMANI

Example 9: Sorting an array using sortedArrayUsingSelector.

#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *myShirts;
NSString *shirts;
int i;
int count;
myShirts = [NSMutableArray arrayWithObjects: @"CK", @"AF", 
@"GUCCI", @"ARMANI", @"CK", nil];
NSLog (@"Number of elements in array = %lu", [myShirts count]);
count = [myShirts count];
for (i = 0; i < count; i++)
NSLog (@"Element %i = %@", i, [myShirts objectAtIndex: i]);
NSLog (@"\n");
NSArray *sorted_Arr;
sorted_Arr = [myShirts 
sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSLog (@"Number of elements in array = %lu", [sorted_Arr count]);
count = [sorted_Arr count];
for (i = 0; i < count; i++)
NSLog (@"Element %i = %@", i, [sorted_Arr objectAtIndex: i]);
return 0;
}

Output:
Number of elements in array = 5
Element 0 = CK
Element 1 = AF
Element 2 = GUCCI
Element 3 = ARMANI
Element 4 = CK

Number of elements in array = 5

Element 0 = AF
Element 1 = ARMANI
Element 2 = CK
Element 3 = CK
Element 4 = GUCCI

Pages:1234567...25»