Making an array of Objects in Objective-C. Making an array of Objects in Objective-C. objective-c objective-c

Making an array of Objects in Objective-C.


Arrays in Objective-C with Cocoa/CocoaTouch are quite different from Java. The three main differences are:

  1. The Obj-C arrays are just classes like any other, and have no built-in support in the language. In Java, arrays have a built-in support in the language through the [] operators.

  2. The Obj-C arrays are untyped, they take any object that inherits from NSObject. This means that you can mix types within the same array, although this is probably bad practice unless you have a very good reason to. (Note that int, double, char, BOOL and other built-in types do not inherit from NSObject, so you'd have to wrap them in NSNumber objects if you ever wanted to hold them in an array.)

  3. The Obj-C arrays don't really have a concept of a fixed length, unless the whole array is immutable. So you don't need to decide on length when you create the array.

To create a mutable array, i.e. one where you're allowed to change the objects (and even add and remove objects, changing the array's length), you create an NSMutableArray:

myArray = [[NSMutableArray alloc] init];// Could also use initWithCapacity: 40, but not necessary.

To add an object, such as an instance of your Person, you call the addObject: method, either making a new Person right there, or giving it a pointer to an existing one:

[myArray addObject: [[[Person alloc] init] autorelease]];[myArray addObject: someOtherPerson];// Last line adds some other person I already had a pointer to.

To remove an object, you can use removeObjectAtIndex: or removeLastObject. To replace, you call replaceObjectAtIndex:withObject:. To insert an object in the middle of your array (and remember, this will also increase the size of the array), you call insertObject:atIndex:.

That covers the most important mutating methods, i.e. those specific to NSMutableArray. Of course, you also have the methods that only observes an array, and those are defined in NSArray. The most important ones are count, giving the current number of elements, and objectAtIndex:, giving the object at the index you provide.

Supposing you have some Person objects in your array, you can iterate over them like this:

for (int i = 0; i < [myArray count]; i++) {    Person* p = [myArray objectAtIndex: i];    // Do something with p }

You might wonder how you'd create an NSArray as such, if you're not allowed to add to it. Well, one of its constructors is initWithObjects:, which allows you to write a comma-separated list of objects to add (and you need to end with a nil value). Another is initWithArray:, which allows you to pass another array (which could be mutable), and the constructor will use the objects from the provided array when making the new one.


Here is an example that creates an NSMutableArray instance variable in the Controller class and adds a Person object to that array each time you evoke doSomeWork:

@interface ControllerNSMutableArray *personArray;@end@implementation Controller- (void) viewDidLoad {    ................    NSMutableArray *personsArray = [[NSMutableArray alloc] initWithCapacity:40];}- (IBAction)doSomeWork {    Person *myPerson = [[Person alloc] init];    myPerson.name = name;    myPerson.age = age;    [personsArray addObject:myPerson];}@end