C# to Objective-C - Part 2 : Arrays

Stephen Zaharuk / Tuesday, November 20, 2012

Today we'll continue our exploration into objective-c by looking at another common data type that you'll use heavily but has a slightly different syntax than C#: Arrays. 

Before we dive right it in its worth noting that the goal of these posts is to be a reference/tutorial tool that will help a developer familiar with programming in C#, get up and running in objective-c in very little time. Enjoy!

Arrays

When working with arrays, you first need to decide if your array is going to be readonly. If so, you should use NSArray  otherwise, if you want to be able to add/remove/insert etc, you should use an NSMutableArray.

To keep things simple, the C# code snippets use an array called a generic list.

Declaring an Array with values

C#:
List data = new List(new []{"one", "two"});

Objective-C: (Note when defining an array with a comma delimited list of objects, you MUST end it with nil.)
NSArray* data = [NSArray arrayWithObjects:@"one", @"two", nil];


Declaring an empty Array

C#:
List data = new List();

Objective-C:
NSMutableArray* data = [[NSMutableArray alloc]init];


Adding to an array

C#:
List data = new List();
data.Add("One");


Objective-C:
NSMutableArray* data = [[NSMutableArray alloc]init];
[data addObject:@"one"];


Inserting into an Array

C#:
List data = new List();
data.Insert(0, "One");

Objective-C:
NSMutableArray* data = [[NSMutableArray alloc]init];
[data insertObject:@"one" atIndex:0];


Removing from an Array

C#:
data.Remove("one");
data.RemoveAt(0);
data.RemoveAt(data.Count -1);

Objective-C:
[data removeObject:@"one"];
[data removeObjectAtIndex:0];
[data removeLastObject];


Length of an Array

C#:
data.Count

Objective-C:
data.count
[data count]


Accessing an object from an Array

C#:
string val = data[0];

Objective-C:
NSString* val = [data objectAtIndex:0];


Creating an Array with numbers

C#:
List data = new List();
data.Add(1);

Objective-C:
NSMutableArray* data = [[NSMutableArray alloc]init];
[data addObject:@1];
[data addObject:[NSNumber numberWithInt:2]];


Looping through an Array

C#:
foreach(string val in data)
{
   // Do Something with val.
}

Objective-C: (More info)
for(NSString* val in data)
{
   // Do Something with val.
}


Checking for the existence of an object in an Array

C#:
bool exists = data.Contains("one");

Objective-C:
BOOL exists = [data containsObject:@"one"];


Find the index of an object.

C#:
int index = data.IndexOf("one");

Objective-C:
int index = [data indexOfObject:@"one"];


Sorting

C#:
data.Sort();

Objective-C:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
data = [data sortedArrayUsingDescriptors:sortDescriptors];


Remove all objects from the Array

C#:
data.Clear();

Objective-C:
[data removeAllObjects];

As always, I hope this was helpful! In the next post we'll actually start digging into some of the meat of objective-c and we'll create classes, methods and properties

Also, If you're brand new to the platform or even a seasoned veteran, you should check out our brand new iOS native product: NucliOS

If you haven't already check out our samples app:

By Stephen Zaharuk (SteveZ)