Well the solution was a bit more involved than I was expecting, but
if used as a base class for custom classes, it offers a standardized
way to add globals to any class. It handles subclass globals by
creating separate dictionaries for them and releases these
dictionaries when all instances of a class have been released.
One thing I'm not sure about is how this base class would behave in a
threaded environment. Anyone see any thread-related gotchas in the
following code?
- (void) dealloc
{
NSString *className = NSStringFromClass([self class]);
NSNumber *instanceCounter = [gCounters objectForKey: className];
int counter = [instanceCounter intValue] ;
counter--;
if (counter == 0)
{
// no more objects of this class
// delete its counter and globals
[gCounters deleteObjectForKey: className];
[gGlobals deleteObjectForKey: className];
}
// if gCounters is empty, there are no more instances
// of any type so release gGlobals and gCounters
if ([gCounters count] == 0)
{
[gCounters release];
[gGlobals release];
}
[super dealloc];
}