Re: Weird calculation
Re: Weird calculation
- Subject: Re: Weird calculation
- From: Charles Srstka <email@hidden>
- Date: Thu, 24 Jan 2002 20:43:08 -0600
On Thursday, January 24, 2002, at 02:21 PM, Erik M. Buck wrote:
Also, how should one do this:
switch([[NSUserDefaults standardUserDefaults] objectForKey:@"someKey"])
{
enum someSetOfPreferences {
doOneThingByDefault;
doAnotherThingByDefault;
askUserEachTime;
}
I would say that the object obtained from [[NSUserDefaults
standardUserDefaults] objectForKey:@"someKey"] houlkd have a method
called -doSomething. Use polymorphism to obtain the different behaviors
depending on the particular object obtained. Various design patterns
can be
used to add even more flexibility and reduce coupling even more. The
code
that you suggest below is very fragile. If another option is ever
added to
the defaults then the code will break.
However, I am not the sole provider of programming wisdom. Do whatever
you
want and live with the consequences. It is your life and your maintence
hassle. I personally think that loosest possible coupling combined with
fewer lines of code is a win-win situation. If you are using a switch
statement, you are "probably" not achiveing loose coupling or fewer
lines of
code.
I'm sorry, I should have been more clear. The code fragment above should
have started with switch([[[NSUserDefaults standardUserDefaults]
objectForKey:@"someKey"] intValue]). The object returned by the defaults
is an NSNumber, and it can contain a value of 0,1, or 2, corresponding
to three different user settings. I'm going to have the same object do
something in each case, but it will have three different behaviors
depending on what this number is. I don't think it's possible for me to
put objects other than NSNumber, NSString, and NSData in a .plist file,
so I'm stuck with the NSNumber. What I would do right now if I added
another option would be to add that option in the appropriate place in
the enumerator.
How could I do this? I'm not just trying to be argumentative - if
there's a better way to do this, I'd like to know what it is.