On Aug 29, 2008, at 8:54 PM, Brad Gibbs wrote:

I'm having a hard time with what should be a simple task - storing an integer for a gradient angle as a user default and then updating the screen. When I quit the app and open it again, the NSTextField shows the last value I set for the gradient, but with the following code:

- (IBAction)changeElementBarGradientAngle:(id)sender {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSLog(@"gradient angle is %d", [elementBarGradientAngleTextField intValue]); [defaults setInteger:[elementBarGradientAngleTextField intValue] forKey:ICNElementBarGradientAngleKey];

In this line, ICNElementBarGradientAngleKey is being used as a key into the defaults dictionary. I don't know what its value is, but I suspect its a name.


NSLog(@"Element bar angle is now: %d", [ICNElementBarGradientAngleKey intValue]);

In this line, you're not looking up the value using the key. Instead, you're asking the key string to convert itself into an integer. If the key string is not numerical itself, it will return 0.

Consider the following:

[@"Hello" intValue];

vs.

[@"123" intValue];

Neither of these has anything to do with looking up a value in the user defaults. They are just attempts to parse a string and obtain an integer. The first produces 0 because @"Hello" doesn't contain an integer value, while the second produces 123. Your usage corresponds to the @"Hello" case.

To look up the value of a default, you do the following:

[[NSUserDefaults standardUserDefaults] integerForKey:ICNElementBarGradientAngleKey]


Cheers,
Ken

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to