• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Newb Question re NSUserDefaults and Ints
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newb Question re NSUserDefaults and Ints


  • Subject: Re: Newb Question re NSUserDefaults and Ints
  • From: Brad Gibbs <email@hidden>
  • Date: Fri, 29 Aug 2008 19:36:59 -0700

Thanks.

I'd read that dictionaries and plists were particular in the types they accept, but I was looking at page 201 of Hillegass (3rd edition), which shows:

- (void)setInteger:(int)value forKey:(NSString *)defaultName

and

- (int)integerForKey:(NSString *)defaultName

and blindly followed....  Apple's NSUserDefaults documentation shows:

- (void)setInteger:(NSInteger)value forKey:(NSString *)defaultName


which makes more sense. Must be a typo in the book?



On Aug 29, 2008, at 7:14 PM, Andrei Kolev wrote:

Brad,

You can't store an int into a Dictionary or user defaults. For the objects you can use, see here:

http://developer.apple.com/documentation/Cocoa/Conceptual/PropertyLists/Articles/AboutPropertyLists.html#/ /apple_ref/doc/uid/20001010

NSNumber and NSString should work.

Also, [ICNElementBarGradientAngleKey intValue] will try to turn the key into a number, not get it's value. You want to use:
[[defaults objectForKey: ICNElementBarGradientAngleKey] intValue];


Andrei

On 30.08.2008, at 04:54, 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];
NSLog(@"Element bar angle is now: %d", [ICNElementBarGradientAngleKey intValue]);
}


I get:

2008-08-29 18:42:10.627 Icon[35645:10b] gradient angle is 75
2008-08-29 18:42:10.628 Icon[35645:10b] Element bar angle is now: 0

I've also tried turning the int into an NSNumber object and using defaults setObject: foKey: without success.

It seems as though I have to use:

[gradient drawInRect:[self bounds] angle: [ICNElementBarGradientAngleKey intValue]];


turning ICNElementBarGradientAngleKey back into an int, which makes me wonder, is setInteger in:


[defaults setInteger:[elementBarGradientAngleTextField intValue] forKey:ICNElementBarGradientAngleKey];


meant to be used with an int, or an object, such as NSUInteger? What am I doing wrong?



Thanks,

Brad
_______________________________________________

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:
@mac.com


This email sent to email@hidden


_______________________________________________

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


  • Follow-Ups:
    • Re: Newb Question re NSUserDefaults and Ints
      • From: Brad Gibbs <email@hidden>
References: 
 >Newb Question re NSUserDefaults and Ints (From: Brad Gibbs <email@hidden>)
 >Re: Newb Question re NSUserDefaults and Ints (From: Andrei Kolev <email@hidden>)

  • Prev by Date: Newb Question re NSUserDefaults and Ints
  • Next by Date: Cancel application terminate on Logout/Shut Down
  • Previous by thread: Re: Newb Question re NSUserDefaults and Ints
  • Next by thread: Re: Newb Question re NSUserDefaults and Ints
  • Index(es):
    • Date
    • Thread