« The Storage Series, Part One

Posted by Andy Monitzer on March 07, 2001 [Feedback (0) & TrackBack (1)]

// Part One: Overview

One of the most basic "pieces" of any program is the data inside of it or that which is entered into it, either by a user, reading a file from a hard disk, and so on. Data is composed of things like integers (3, 25, -31386), floats (2.245, -11/7), strings (@"Bob", @"Susan P. Barnes"), and so on.

For example, you may want to keep an address book in your software. A simple address book is nothing more than a list of contacts. Each contact has a first name, last name,telephone number, and a birthday (this is an extremely simple example, of course). This list should be dynamically extensible, efficient, and easy to access.

In programming languages like C or C++, you have to do all the work for yourself. This often means you duplicate a lot of your efforts. Java, on the other hand, provides six different storage interfaces, each of which have numerous classes. Cocoa takes a much easier approach: there are only two different classes, or types, which have proven to be enough for almost every case.

The two types are arrays and dictionaries. Arrays are a list of items in a particular order, and dictionaries map keys to values (e.g. name="Andy", telephone="555-4711", etc.).

Two types may not seem like a lot, but when you consider that you can arrays and dictionaries in any way you like, you begin to realize how powerful they can become. For example, imagine creating an array of Contacts for your address book. Each contact could have keys for "name" and "telephone," but each item in the array could represent a person. Conceptually, that might look like:

First Contact = {name = "Bob Jones";
                 telephone = "555-6626";
                 address = "123 Somewhere Street";
                 email = "bob@jones.com";}

Second Contact = {name = "John Doe";
                  telephone = "555-1232";
                  address = "321 Elsewhere Drive";
                  email = "john@doe.com";}

You can see, then, how we might easily access the second person's email address. Conceptually, again, it'd be as simple as finding the second person's contact info, and then reading the value associated with the "email" key. In this case, "john@doe.com".

But... there's one more thing: "mutable" means dynamically changeable. An array or a dictionary can be made to be mutable in Objective-C. In other words, mutable arrays and dictionaries are not static, unchanging blocks of data, but living blocks of data that can be changed, updated, and modified.

When is it appropriate to use mutable versus non-mutable arrays or dictionaries? In the case of an address book, "mutable" would probably be your choice, as you may wish to update John Doe's phone number. in the case of an array of state capitals, however, non-mutability is quite a fine choice: state capitals don't change very often, and if one ever did, you could just make the change in your code.

That's the end of this short introduction to arrays and dictionaries. Be sure to check our Tutorials page for more on arrays and dictionaries!

Post a comment