Tips for the C# Coder: ArrayList != NSArray

One of my favorite collection containers in .NET (1.1) was the ArrayList. Then .NET 2.0 came around and generic lists were introduced i.e. the List<T> class. Man was I impressed.

Enter the iPhone SDK and Objective-C. I quickly learned that things were slightly different.

When coding for the iPhone SDK, you are provided with two classes that are similar to our trusted little collection friends. These are the NSArray and its counter part, the NSMutableArray.

The NSArray provides all the low level fun features that you’d expect in a collection class. However, once you have created an instance of the NSArray you cannot add or remove objects. This is where the NSMutableArray comes in.

The NSMutableArray is a sub-class of the NSArray. It provides the functionality to modify the collection once the instance has been created. With the NSMutableArray, you can add, insert, remove and replace objects from list. Hence the “mutable” in the name.

BOTTOM LINE

NSArray: you can’t change the collection once the collection has been created.

NSMutableArray: you can modify the collection after the collection has been created.

GEEK FACTS

  1. To quickly create an array that “behaves” like an ArrayList or List<T> collection, use a NSMutableArray.
  2. To initialize a NSMutableArray, do this:

    NSMutableArray *array = [[NSMutableArray alloc] init];

  3. To create a new NSMutableArray from an existing array, do this:

    NSMutableArray *array = [[NSMutableArray alloc] initWithArray:someOtherArray];

  4. To add one array to an existing array (that may or may not already have data), do this:

    [oldArray addObjectsFromArray:newArray];

  5. To add a single object to a NSMutableArray:

    [oldArray addObject:yourNewObject];

  6. To delete all the objects of a NSMutableArray, do this:

    [oldArray removeAllObjects];

Let me know if this helps. Drop me a comment or ping me on twitter www.twitter.com/haroldcampbell

-H

Advertisements

Tags: , ,

3 Responses to “Tips for the C# Coder: ArrayList != NSArray”

  1. Varun Says:

    Thanks, this definitely helped. I’m new to Cocoa, and coming from the .NET realm, this definitely saved me a whole lot trial and error.

  2. The Treatment Of Genital Herpes Is Directed At Says:

    Everything is very open with a clear explanation of the issues.

    It was really informative. Your site is useful.
    Thank you for sharing!

  3. Alberton Gas Geysers South Africa Says:

    Wow, this article is good, my sister is analyzing these things, thus I am going to let know
    her.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: