views:

31

answers:

2

I'm looking for a nice way to set my configuration constants. My idea was to create a singleton with my config properties. Say mySingletonConf with: URL, USERID, PASSWORD. During the initialization of the sharedInstance, a configuration file should be read to set the properties in mySingletonConf.

  1. Should I use properties for this type of constants? I take they should be class-level properties?
  2. Is it possible to set the configuration dynamically? I. e. by reading all Setter-Methods of mySingletonConf, then searching the loaded configuration (.plist-Dictionary) for the key == property name and then invoke the settter with the value?

It would be nice to have the things set dynamically, in case new constants are needed. Then I would just have to create new properties and adjust the configuration files. Am I on the right track? Thanks for any help!

A: 

NSUserDefaults can take care of a lot of stuff and it ensures that the configuration is user-specific, so if multiple users on the same Mac use your program they can configure your program independently. There is also an object in Interface Builder for binding your user interface elements to, making things even easier. If you do want to make your configuration system-wide, you should use the Core Foundation Preference Utilities.

For storing passwords, you can use Apple's Keychain Services. A user is able to specify which programs are allowed to use the stored password (which would ideally be just yours). Storing passwords in NSUserDefaults is also an option if the password is not of any particular importance.

Don't re-invent the wheel; application-wide user-or-host-specific configuration services are provided for you.

dreamlax
Thanks a lot for your answer! I'm using the .plist to read the settings from. The .plist contains numerous Test-parameters. Now I'm at the point, where I have the settings-dictionary and I don't really want to [configDict objectForKey:paramName] for every single test-parameter in every TestCase. My idea was to load all parameters once and then use the configuration as constants (PARAM_A) or something like this: [myTestConf paramA]; Is it possible?
jelenaasche
A: 

Well, it is possible :) I ended up writing the mentioned Singleton, which has readonly public properties and readwrite access from within the class (used Categories for that, see private setter example).

The vars of the class are filled with values from the .plist file during the initialization. I used the Runtime API to get the list of variables (just search for "objective-c list of variables" on Stackoverflow) and get the value from the loaded.plist dictionary using the var name as key.

The the values can be use almost as constants:

MyConstants* testConstants = [MyConstants sharedInstance];
NSLog(testConstants.PARAM1);
jelenaasche