Mysterious NULL Coming From NSUserDefaults


i've been enjoying nsuserdefaults when going through hillegeass book.

i've decided small app has 2 colors stores. these colors colorwell via preference pane.

have bounded values of wells backgroundwell , linewell in ib.

have appropriately named methods. know bindings ok becuase if return [nscolor whitecolor] instead of data
nsuserdefaults, they're yellow when open pane. that's ok.

pretty sure i'm getting null defaults because wells black. have no "blackcolor" in project. searched that. ;)

doing wrong? thought nsuserdefaults pretty simple...

below getters , setters.
code:
  -(nscolor*)backgroundwell {         nslog(@"called bgwell");      nsuserdefaults* defaults = [nsuserdefaults standarduserdefaults];     nsdata* data = [defaults objectforkey:@"bgcolor"];     nscolor* newcolor = [nskeyedunarchiver unarchiveobjectwithdata:data];     nslog(@"retrieved color bg %@", 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 %@", 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 %@", newcolor);     nsuserdefaults* defaults = [nsuserdefaults standarduserdefaults];     nsdata* colordata = [nskeyedarchiver archiveddatawithrootobject:[linewell color] ];     [defaults setobject:colordata forkey:@"linecolor"];     nslog(@"changing line color");     }

-------
below +(void) initialize method

code:
+(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);     }[/
size]


edit:
need add something: tried reading nsstring prefs , worked fine. it's due writing nsdata...
 



Forums Macs Mac Programming


  • iPhone
  • Mac OS & System Software
  • iPad
  • Apple Watch
  • Notebooks
  • iTunes
  • Apple ID
  • iCloud
  • Desktop Computers
  • Apple Music
  • Professional Applications
  • iPod
  • iWork
  • Apple TV
  • iLife
  • Wireless

Comments

Popular posts from this blog

Upgrade Eligibility?

need help with itunes 7.7

How to change Apple IDs on iPhones