OK to access instance variables through class methods?
OK to access instance variables through class methods?
- Subject: OK to access instance variables through class methods?
- From: Julian Vrieslander <email@hidden>
- Date: Thu, 01 Apr 2004 15:01:29 -0800
I have a controller class which manages a group of settings for an
application. These settings are stored in a mutable dictionary, an instance
variable of the controller object.
@interface ZNSetupsController : NSWindowController
{
NSMutableDictionary *activeSetup;
...
}
+ (ZNSetupsController *)controller;
- (id)initWithSetupsPath:(NSString *)path;
- (void)setObject:(id)value forKey:(NSString *)key
- (id)objectForKey:(NSString *)key
...
@end
I only create one instance of the ZNSetupsController, which lives throughout
the life of the program, and is owned by my application controller. When I
need to access variables in the activeSetup, I can do this:
// Get a reference to the single instance of ZNSetupsController
ZNSetupsController *theController = [ZNSetupsController controller];
// Use controller to access settings
NSString *foo = [controller objectForKey:ZN_FooKey];
[controller setObject:@"bar" forKey:ZN_BarKey];
This looks like the scheme for accessing NSUserDefaults (but there are
reasons why I am not storing the setup data there).
My question is this: since I am only creating one instance of the
ZNSetupsController, why not implement the key-value accessors as class
methods?
// No need to get reference to controller object
NSString *foo = [ZNSetupsController objectForKey:ZN_FooKey];
[ZNSetupsController setObject:@"bar" forKey:ZN_BarKey];
Is there anything wrong with doing it this way?
--
Julian Vrieslander <email@hidden>
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.