Arrays, sets and dictionaries

As objects are accessible by their identifiers only, we have to make sure to remember them. If we forget about this id, we cannot send messages to the object anymore. The object would just be lost, albeit still reside alive in the computer's memory. In case we have to deal with only a limited number of objects, we can have them remembered in named variables (see section 7.2). But as soon as we create dozens, hundreds or even thousands of objects, this approach becomes infeasible.

Simplest way to store object ids are lists or arrays. You put your objects into the list, then only remember the list's id. By accessing the list you will get to your objects again.

Several types of lists will help you to put your objects in order: NSArray(9.3), NSSet(9.4) and NSDictionary(9.5). These classes are defined in the GNUstep Foundation and accessible by the Smalltalk interpreter (MolTalk) as well as compiled Objective-C programs (libmoltalk).

A short form of array construction in Smalltalk is: #(element1,element2,element3). Use this only if you will forever keep your code in Smalltalk, as this form does not exist in Objective-C.

Example:

fixedArray := #(1 3 4 5 99 -3 1.2).     " creates an array of some numbers "

myArray := NSMutableArray new.     " create an array of class NSMutableArray, which can be altered "

myArray addObject: myFonNumber.     " add an object to the array "

moltalk@moltalk.org      version of this document: V3.0