Home > Articles > Home & Office Computing > Mac OS X

  • Print
  • + Share This
From the author of

The address book also feels slightly strange when you start using it. This is a function of its age. It predates the rewrite of key-value coding and the introduction of key-value observing, and so doesn't support either.

The address book framework introduces some new collection classes. These are similar to sets and dictionaries, but with some important differences. Unlike the standard Cocoa collection classes, the ones provided by the keychain are implicitly typed. The type of the first object that you insert into the collection defines the type of the collection. All other objects must have a matching type.

The most important class in the address book is ABPerson, which represents a person. This is a sublcass of ABRecord, as is ABGroup, which represents a group of people. ABRecord defines three methods that look a lot like the ones that you use for key-value coding. These are:

  • (BOOL)setValue: (id)value forProperty: (NSString*)property;
  • (BOOL)removeValueForProperty: (NSString*)property;
  • (id)valueForProperty: (NSString*)property;

Unfortunately, although these look like KVC methods, they are not and they do not support KVO. You can think of an ABRecord like a dictionary, with a few slight differences. All properties are typed and the type is set when the property is created.

A lot of properties in the address book might have multiple values. A single person might have several telephone numbers or email addresses. These are all stored in an ABMultiValue instance, which is a bit like a dictionary and a bit like an array. It is an ordered collection of key-value pairs, where the key is always a string and the value is typed. Every value in a multivalue must have the same type as the first one inserted.

The multivalue class does support fast enumeration, but ABRecord does not. You can use a for..in loop to iterate over a multivalue, but not to iterate over all of the properties of a person or group.

Another class that postdates the Address Book framework is NSPredicate. This was introduced with 10.4 and provides an abstract way of representing queries. The address book uses ABSearchElement, which serves the same purpose but has a very different interface.

I was hoping that Apple would tidy up the Address Book framework with 10.6 — or deprecate it and add a more modern interface — but unfortunately they did not. It's one of the uglier corners of Cocoa, but it's quite a small framework so you generally won't have to spend much time with it.

  • + Share This
  • 🔖 Save To Your Account