Re: Mysterious NULL Coming From NSUserDefaults

2008-07-07 Thread Alex Wait
I knew that naming my variables like that was a bad idea. :)
Thanks! I changed the names and updated what needed to be updated because of
the name changes and it works now!

w00t!

Thanks again!
Alex

On Sun, Jul 6, 2008 at 10:52 PM, Boaz Stuller [EMAIL PROTECTED] wrote:

 Well, I see a couple problems here
 1) Even though you're getting directly passed a color in your setters,
 you're ignoring that and trying to find out the color from the wells. That's
 bad form for many reasons, and if those backGroundWell and lineWell
 variables weren't hooked up correctly, that would explain your symptoms.
 Especially since your lineWell and backGroundWell aren't getting hooked up
 correctly.

 2)  Since I can't see your whole project, I can't be 100% sure, but I'm 99%
 sure your lineWell and backGroundWell probably aren't getting hooked up
 correctly.  This is because you named your color setters and getters *the
 exact same thing* as the instance variables for your color wells.  This is
 a big mistake, for reasons that are way too complicated to explain in an
 email.  The short version is that when nibs are loaded, outlets are set
 using key-value coding.  If you have a method named -setLineWell: and an
 ivar named lineWell, the KVC routines assume that the -setLineWell: method
 is the way to set the lineWell ivar and call that method instead of setting
 lineWell directly.  You can avoid this problem in the future by making your
 getter and setter names describe what you're actually getting and setting,
 i.e -lineWell and -setLineWell: should get/set an NSColorWell, while
 -lineColor and -setLineColor: should get/set an NSColor.

 3) Pulling back a bit, you're working way too hard.  You don't have to
 write any code at all to hook a color well up to a preference key.  You just
 bind to the 'Shared User Defaults Controller', set the controller key to
 'values', the Model Key to whatever you want your preference key to be, and
 the Value Transformer to NSUnarchiveFromData (it'll be in the popup
 menu).  It will handle all that archiving and unarchiving you're currently
 doing manually.

 4) The G in Background should not be capitalized. ;)

 Best wishes,
 Bo




-- 
If you can't be kind, at least have the decency to be vague.
___

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]


Mysterious NULL Coming From NSUserDefaults

2008-07-06 Thread Alex Wait
I've been enjoying NSUserDefaults when going through the Hillegeass book.

I've decided to do a small app that has two colors it stores. I get these
colors from a colorwell via  Preference Pane.

I have bounded the values of the wells to backGroundWell and lineWell in IB.

I have the appropriately named methods. I know my bindings are ok becuase if
I just return [NSColor whiteColor] instead of the data
from NSUserDefaults, they're yellow when I open the pane. so that's ok.

I am pretty sure I'm getting a null from the defaults because the wells are
black. I have no blackColor in my project. Already searched for that. ;)

What am I doing wrong? I thought that NSUserDefaults was pretty simple...

Below are my getters and setters.

-(NSColor*)backGroundWell
{

NSLog(@called BGwell);

NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSData* data = [defaults objectForKey:@BGCOLOR];
NSColor* newColor = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@retrieved Color for BG is %@, newColor);
//NSLog(@%@,[NSKeyedUnarchiver unarchiveObjectWithData:data]);
return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
-(NSColor*)lineWell
{

NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSData* data = [defaults objectForKey:@LINECOLOR];
return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}

-(void)setBackGroundWell: (NSColor*)newColor
{
NSLog(@New Color is %@, newColor);
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSColor* color = [backGroundWell color];
NSData* colorData = [NSKeyedArchiver archivedDataWithRootObject:color];
[defaults setObject:colorData forKey:@BGCOLOR];
NSLog(@changing background color);
}
-(void)setLineWell: (NSColor*)newColor
{
NSLog(@New Color is %@, newColor);
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSData* colorData = [NSKeyedArchiver
archivedDataWithRootObject:[lineWell color] ];
[defaults setObject:colorData forKey:@LINECOLOR];
NSLog(@changing line color);

}


---
Below is my +(void) initialize method

+(void) initialize
{

NSMutableDictionary* defaultValues = [NSMutableDictionary dictionary];

NSData* backgroundColorData = [NSKeyedArchiver
archivedDataWithRootObject:[NSColor greenColor] ];
NSData* lineColorData = [NSKeyedArchiver
archivedDataWithRootObject:[NSColor whiteColor] ];

[defaultValues setObject:backgroundColorData forKey:@BGCOLOR];
[defaultValues setObject:lineColorData forKey:@LINECOLOR];

[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];
NSLog(@registered defaults %@, defaultValues);

}


-- 
If you can't be kind, at least have the decency to be vague.
___

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]


Re: Mysterious NULL Coming From NSUserDefaults

2008-07-06 Thread Alex Wait
Indeed. Both the setters and getters are being called. I looked in the plist
file for my app and I see BgColor and LineColor
as values with a bunch of digits/hex for its value.

On Sun, Jul 6, 2008 at 10:13 PM, Thomas Mueller 
[EMAIL PROTECTED] wrote:

 Hi,

 Are the setters actually being called? Maybe the values are never
 being stored in the defaults? Maybe have a look at the preferences
 file for your app (in ~Library/Preferences) to see if anything is
 stored in there. I guess you won't be able to see *which* color is
 stored, though.

 Just an idea; I'm just a few chapters ahead of you, so I'm probably
 wrong... :-)

 Regards,
 Thomas


 2008/7/7 Alex Wait [EMAIL PROTECTED]:
  I've been enjoying NSUserDefaults when going through the Hillegeass book.
 
  I've decided to do a small app that has two colors it stores. I get these
  colors from a colorwell via  Preference Pane.
 
  I have bounded the values of the wells to backGroundWell and lineWell in
 IB.
 
  I have the appropriately named methods. I know my bindings are ok becuase
 if
  I just return [NSColor whiteColor] instead of the data
  from NSUserDefaults, they're yellow when I open the pane. so that's ok.
 
  I am pretty sure I'm getting a null from the defaults because the wells
 are
  black. I have no blackColor in my project. Already searched for that.
 ;)
 
  What am I doing wrong? I thought that NSUserDefaults was pretty simple...
 
  Below are my getters and setters.
 
  -(NSColor*)backGroundWell
  {
 
 NSLog(@called BGwell);
 
 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
 NSData* data = [defaults objectForKey:@BGCOLOR];
 NSColor* newColor = [NSKeyedUnarchiver unarchiveObjectWithData:data];
 NSLog(@retrieved Color for BG is %@, newColor);
 //NSLog(@%@,[NSKeyedUnarchiver unarchiveObjectWithData:data]);
 return [NSKeyedUnarchiver unarchiveObjectWithData:data];
  }
  -(NSColor*)lineWell
  {
 
 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
 NSData* data = [defaults objectForKey:@LINECOLOR];
 return [NSKeyedUnarchiver unarchiveObjectWithData:data];
  }
 
  -(void)setBackGroundWell: (NSColor*)newColor
  {
 NSLog(@New Color is %@, newColor);
 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
 NSColor* color = [backGroundWell color];
 NSData* colorData = [NSKeyedArchiver
 archivedDataWithRootObject:color];
 [defaults setObject:colorData forKey:@BGCOLOR];
 NSLog(@changing background color);
  }
  -(void)setLineWell: (NSColor*)newColor
  {
 NSLog(@New Color is %@, newColor);
 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
 NSData* colorData = [NSKeyedArchiver
  archivedDataWithRootObject:[lineWell color] ];
 [defaults setObject:colorData forKey:@LINECOLOR];
 NSLog(@changing line color);
 
  }
 
 
  ---
  Below is my +(void) initialize method
 
  +(void) initialize
  {
 
 NSMutableDictionary* defaultValues = [NSMutableDictionary dictionary];
 
 NSData* backgroundColorData = [NSKeyedArchiver
  archivedDataWithRootObject:[NSColor greenColor] ];
 NSData* lineColorData = [NSKeyedArchiver
  archivedDataWithRootObject:[NSColor whiteColor] ];
 
 [defaultValues setObject:backgroundColorData forKey:@BGCOLOR];
 [defaultValues setObject:lineColorData forKey:@LINECOLOR];
 
 [[NSUserDefaults standardUserDefaults]
 registerDefaults:defaultValues];
 NSLog(@registered defaults %@, defaultValues);
 
  }
 
 
  --
  If you can't be kind, at least have the decency to be vague.
  ___
 
  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/thomasmueller76%40googlemail.com
 
  This email sent to [EMAIL PROTECTED]
 




-- 
If you can't be kind, at least have the decency to be vague.
___

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]


Re: Mysterious NULL Coming From NSUserDefaults

2008-07-06 Thread Alex Wait
Sorry. forgot something.
I should add this: I tried reading NSColorPanel from the file (it's a
NSString) and I WAS able to read that just fine.

I think it has something to do with NSKeyedArchive/NSKeyedUnarchiver

On Sun, Jul 6, 2008 at 10:40 PM, Alex Wait [EMAIL PROTECTED] wrote:

 Indeed. Both the setters and getters are being called. I looked in the
 plist file for my app and I see BgColor and LineColor
 as values with a bunch of digits/hex for its value.


 On Sun, Jul 6, 2008 at 10:13 PM, Thomas Mueller 
 [EMAIL PROTECTED] wrote:

 Hi,

 Are the setters actually being called? Maybe the values are never
 being stored in the defaults? Maybe have a look at the preferences
 file for your app (in ~Library/Preferences) to see if anything is
 stored in there. I guess you won't be able to see *which* color is
 stored, though.

 Just an idea; I'm just a few chapters ahead of you, so I'm probably
 wrong... :-)

 Regards,
 Thomas


 2008/7/7 Alex Wait [EMAIL PROTECTED]:
  I've been enjoying NSUserDefaults when going through the Hillegeass
 book.
 
  I've decided to do a small app that has two colors it stores. I get
 these
  colors from a colorwell via  Preference Pane.
 
  I have bounded the values of the wells to backGroundWell and lineWell in
 IB.
 
  I have the appropriately named methods. I know my bindings are ok
 becuase if
  I just return [NSColor whiteColor] instead of the data
  from NSUserDefaults, they're yellow when I open the pane. so that's ok.
 
  I am pretty sure I'm getting a null from the defaults because the wells
 are
  black. I have no blackColor in my project. Already searched for that.
 ;)
 
  What am I doing wrong? I thought that NSUserDefaults was pretty
 simple...
 
  Below are my getters and setters.
 
  -(NSColor*)backGroundWell
  {
 
 NSLog(@called BGwell);
 
 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
 NSData* data = [defaults objectForKey:@BGCOLOR];
 NSColor* newColor = [NSKeyedUnarchiver unarchiveObjectWithData:data];
 NSLog(@retrieved Color for BG is %@, newColor);
 //NSLog(@%@,[NSKeyedUnarchiver unarchiveObjectWithData:data]);
 return [NSKeyedUnarchiver unarchiveObjectWithData:data];
  }
  -(NSColor*)lineWell
  {
 
 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
 NSData* data = [defaults objectForKey:@LINECOLOR];
 return [NSKeyedUnarchiver unarchiveObjectWithData:data];
  }
 
  -(void)setBackGroundWell: (NSColor*)newColor
  {
 NSLog(@New Color is %@, newColor);
 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
 NSColor* color = [backGroundWell color];
 NSData* colorData = [NSKeyedArchiver
 archivedDataWithRootObject:color];
 [defaults setObject:colorData forKey:@BGCOLOR];
 NSLog(@changing background color);
  }
  -(void)setLineWell: (NSColor*)newColor
  {
 NSLog(@New Color is %@, newColor);
 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
 NSData* colorData = [NSKeyedArchiver
  archivedDataWithRootObject:[lineWell color] ];
 [defaults setObject:colorData forKey:@LINECOLOR];
 NSLog(@changing line color);
 
  }
 
 
  ---
  Below is my +(void) initialize method
 
  +(void) initialize
  {
 
 NSMutableDictionary* defaultValues = [NSMutableDictionary
 dictionary];
 
 NSData* backgroundColorData = [NSKeyedArchiver
  archivedDataWithRootObject:[NSColor greenColor] ];
 NSData* lineColorData = [NSKeyedArchiver
  archivedDataWithRootObject:[NSColor whiteColor] ];
 
 [defaultValues setObject:backgroundColorData forKey:@BGCOLOR];
 [defaultValues setObject:lineColorData forKey:@LINECOLOR];
 
 [[NSUserDefaults standardUserDefaults]
 registerDefaults:defaultValues];
 NSLog(@registered defaults %@, defaultValues);
 
  }
 
 
  --
  If you can't be kind, at least have the decency to be vague.
  ___
 
  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/thomasmueller76%40googlemail.com
 
  This email sent to [EMAIL PROTECTED]
 




 --
 If you can't be kind, at least have the decency to be vague.




-- 
If you can't be kind, at least have the decency to be vague.
___

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]


Re: Mysterious NULL Coming From NSUserDefaults

2008-07-06 Thread Boaz Stuller
Well, I see a couple problems here
1) Even though you're getting directly passed a color in your setters,
you're ignoring that and trying to find out the color from the wells. That's
bad form for many reasons, and if those backGroundWell and lineWell
variables weren't hooked up correctly, that would explain your symptoms.
Especially since your lineWell and backGroundWell aren't getting hooked up
correctly.

2)  Since I can't see your whole project, I can't be 100% sure, but I'm 99%
sure your lineWell and backGroundWell probably aren't getting hooked up
correctly.  This is because you named your color setters and getters *the
exact same thing* as the instance variables for your color wells.  This is a
big mistake, for reasons that are way too complicated to explain in an
email.  The short version is that when nibs are loaded, outlets are set
using key-value coding.  If you have a method named -setLineWell: and an
ivar named lineWell, the KVC routines assume that the -setLineWell: method
is the way to set the lineWell ivar and call that method instead of setting
lineWell directly.  You can avoid this problem in the future by making your
getter and setter names describe what you're actually getting and setting,
i.e -lineWell and -setLineWell: should get/set an NSColorWell, while
-lineColor and -setLineColor: should get/set an NSColor.

3) Pulling back a bit, you're working way too hard.  You don't have to write
any code at all to hook a color well up to a preference key.  You just bind
to the 'Shared User Defaults Controller', set the controller key to
'values', the Model Key to whatever you want your preference key to be, and
the Value Transformer to NSUnarchiveFromData (it'll be in the popup
menu).  It will handle all that archiving and unarchiving you're currently
doing manually.

4) The G in Background should not be capitalized. ;)

Best wishes,
Bo
___

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]