user3078414
user3078414

Reputation: 1937

What is the simplest method for reading user defaults in Cocoa/OSX?

I'm porting to OSX a simple unix application, now using a NSView subclass in a NSWindow. There are three color constants (members of this view @interface) which I prefer not having hard-coded, but definable as user-defaults (and loaded at launch) instead. These are: a, b and c, all of type [NSColor colorWithDeviceRed: green: blue: alpha]. I'm trying to learn how such values can be made read from a user defaults file, which can be written either manually or by means of the defaults system command. Also trying to stay away from digging into Preference Pane Controllers, Color Wells, etc. In X11 this is very simple. I'm trying to figure out the simplest way of achieving it in Cocoa. It's not for an "AppStore" type of app. Is there any working example for such a code or anything similar - couldn't find any on the web? Thanks in advance.

Upvotes: 1

Views: 290

Answers (3)

CRD
CRD

Reputation: 53010

Try reading Apple's Storing NSColor in User Defaults, that along with the documentation for NSUserDefaults should answer most questions. You'll find you can even bind a color well's value direct to user defaults, so you may not need to steer clear of them at all.

HTH

Upvotes: 1

Keith Knauber
Keith Knauber

Reputation: 802

NSUserDefaults, yes...

This is code that I use, I store colors as comma separated rgb values.

- (id)getUserPref:(NSString *)aSelector
{
   id returnVal = nil;
   NSString *val = [[NSUserDefaults standardUserDefaults] objectForKey:aSelector];
   NSArray *vals = [val componentsSeparatedByString:@","];
   float rgba[4];
   // brute force validation of string... funky but safe
   if ( [vals count] <= 4 )
   {
      for ( int idx = 0; idx < [vals count]; idx++ )
      {
         val = [vals objectAtIndex:idx];
         val = [val stringByTrimmingCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"01234567890."] invertedSet]];

         if ( [val length] )
         {
            rgba[idx] = [val floatValue];
            if ( rgba[idx] > 255 || rgba[idx] < 0 )
               break;

            if ( [vals count] == 1 )
            {
               returnVal = [[NSColor colorWithContrastWhite:rgba[0]/255.0 alpha:1.0] retain];
               return returnVal;
            }
            else if ( idx == 2 && [vals count] == 3 )
            {
               returnVal = [[NSColor colorWithContrastRed:rgba[0]/255.0 green:rgba[1]/255.0 blue:rgba[2]/255.0 alpha:1.0] retain];
               return returnVal;            
            }
            else if ( idx == 3 && [vals count] == 4 )
            {
               returnVal = [[NSColor colorWithContrastRed:rgba[0]/255.0 green:rgba[1]/255.0 blue:rgba[2]/255.0 alpha:rgba[3]/255.0] retain];
               return returnVal;            
            }         
         }
      }
   }

   DLogErr(@"bad BGThemeMbox user pref %@", aSelector);

   return nil; // better have a fallback color
}

Upvotes: 3

DarkDust
DarkDust

Reputation: 92414

You're probably looking for the NSUserDefaults class. The documentation has plenty of information and a programming guide.

Upvotes: 0

Related Questions