Re: Newb Question re NSUserDefaults and Ints
Re: Newb Question re NSUserDefaults and Ints
- Subject: Re: Newb Question re NSUserDefaults and Ints
- From: Ken Thomases <email@hidden>
- Date: Fri, 29 Aug 2008 23:01:49 -0500
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 (email@hidden)
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:
This email sent to email@hidden