UserDefaults Key Output?

Discussion in 'Mac Programming' started by Darkroom, Oct 18, 2008.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    i have a slider connected to this action. i've declared the static NSString to use for the default's key. and i want to log the results of the slider using the key...

    Code:
    static NSString *const defaultSizeKEY = @"defaultSizeKEY";
    
    
    - (IBAction)changeSize:(id)sender
    	{
    	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    	[defaults setValue:[NSNumber numberWithInt:[sender intValue]] forKey:defaultSizeKEY];
    	NSLog (@"%@", defaultSizeKEY);
    	}
    
    unfortunately, this only logs "defaultSizeKEY" when the slider is changed.

    Code:
    NSLog (@"@i", [sender intValue]);
    
    this works fine, but i need to access the [sender intValue] in a -(void)mouseDragged: (NSEvent *)event method of a different class.

    Code:
    - (void)mouseDragged:(NSEvent *)event
    	{
    	NSSize size = NSMakeSize(64, 64);
            }
    
    NSMakeSize(64,64) works fine, but these are the number i need changed using the slider that is bound to the defaultSizeKEY string.

    as mentioned above writing NSMakeSize(defaultSizeKEY, defaultSizeKEY) doesn't work, and i can't write NSMakeSize([sender intValue]) because the sender is not connected to this mouseDragged event method...

    *head explodes*
     
  2. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #2
    got it...

    instead of using userdefaults i used getters and setters with key-value coding to make it work...

    *head safe*
     
  3. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #3
    The NSLog call will only show the text "defaultSizeKEY", because that is what you told it to show.

    Are you sure that you want changeSize to change the application's preferences? (Because that is what NSUserDefaults is: The application preferences. That is most certainly a place that you don't want to use to communicate a value from one method to another).
     
  4. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #4
    seems like only way to hold UI settings, no? i mean, a user's moved position of a slider should be kept, right?
     
  5. Krevnik macrumors 68030

    Krevnik

    Joined:
    Sep 8, 2003
    #5
    How long do you need to keep this value, though? Just during the running of the app, or across restarts/etc? If the former, then you don't need something like UserDefaults.
     
  6. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #6
    it's so the slider keeps it's position after a restart.
     
  7. Krevnik macrumors 68030

    Krevnik

    Joined:
    Sep 8, 2003
    #7
    Well, you are using NSUserDefaults in the original code correctly.

    The problem is that your NSLog is incorrect. You are simply logging the key name, and not the value Otherwise it is fine.
     

Share This Page