Associative links seem to do the trick. You can essentially bind some storage to the class object itself. (I use NSString
here, instead of the dictionaries you want to use, just for demonstration.)
superclass:
<h / "> <h /">
Subclass:
#import "Stuper.h" @interface Stub : Stuper @end
#import "Stub.h" @implementation Stub + (NSString *) quote { return @"Call me Ishmael."; } @end
<h / "> <h /">
Attempt:
#import <Foundation/Foundation.h> #import "Stuper.h" #import "Stub.h" int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSLog(@"%@", [Stuper str]); NSLog(@"%@", [Stub str]); [pool drain]; return 0; }
Each class object now has its own string associated with it.
2011-12-05 23: 11: 09.031 SubClassVariables [36254: 903] It was the best time, it was the worst time.
2011-12-05 23: 11: 09.034 SubClassVariables [36254: 903] Call me Ishmael.
The only drawback here is that you will need to call the access method every time you want this object; you do not have a pointer that you can use directly. You can call objc_getAssociatedObject
in the superclass as an accessory, of course, since it has access to key
.
source share