registering Defaults with NSUserDefaults not working

  • Hello.

    Before all, I checked on google and the Docs for info, and I don't see what I might be doing wrong.

    This is what Im doing, following the AppPref example but of course for a desktop app.

    In my  - (void)applicationDidFinishLaunching:(NSNotification *)aNotification{

    I have the following:

    id testValue =[[NSUserDefaults standardUserDefaults] objectForKey:kNotPayedGradientKey];

    if(testValue == nil){ //No values yet registered
    //..Creating NSGradients and Put converting them as NSData
    _notPayedGradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedRed:_notPayedRedColor green:_notPayedGreenColor blue:_notPayedBlueColor              alpha:1.0f]  endingColor:[NSColor colorWithCalibratedRed:_notPayedRedColor + 0.1 green:_notPayedGreenColor + 0.1                  blue:_notPayedBlueColor + 0.1 alpha:1.0f]];

    NSData * notPayedData = [NSKeyedArchiver archivedDataWithRootObject:_notPayedGradient];

    then after creating all the gradients I do:

    NSDictionary * appDefaults = [NSDictionarydictionaryWithObjectsAndKeys:
        notPayedData,kNotPayedGradientKey
        ,partPayedData,kPartPayedGradientKey,
        payedData,kPayedGradientKey,nil];

    [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
    if([[NSUserDefaults standardUserDefaults] synchronize])NSLog(@"Could Save");

    So, I ran my app, and as expected I enter the if statement,  and I see in the console, the  "Could Save" message.

    I ran again, expecting not to enter the if statement, but for my surprise it came in again. the testValue is always nil. I change then:

    NSString * testValue = [NSUserDefaults standardUserDefaults] stringForKey:kNotPayedGradientKey];

    to see if the given key existed, but still nothing. I went to checn the .plist in the /users/<USER>/library/preferences, and there my old com.mycompany.<appName>.plist, but there is nothing inside,  I change then the target identifier to something else, and when running and going into the preferences folder there is no such a ,plist...

    What can I do?, what am I doing wrong?

    Thx in advance any help will be appreciate it.

    Gustavo.
  • On Mar 1, 2010, at 3:01 AM, Gustavo Pizano wrote:

    > [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
    > if([[NSUserDefaults standardUserDefaults] synchronize])NSLog(@"Could Save");

    -registerDefaults: does not actually change default values, it simply sets what the default values should be in certain cases. You can read NSUserDefaultsController about ways to use default values. If you delete your plist preference file, then re-run, you might see different results. You might be happier in the long wrong if you follow the docs and have all of your application defaults in app delegate's +initialize, or as a pre-defined plist in your project. You also don't need to call -synchronize if your app will terminate normally (saving defaults is one of the exit functions).

    HTH,

    Keary Suska
    Esoteritech, Inc.
    "Demystifying technology for your home or business"
  • On Mar 1, 2010, at 4:07 PM, Keary Suska wrote:

    > On Mar 1, 2010, at 3:01 AM, Gustavo Pizano wrote:
    >
    >> [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
    >> if([[NSUserDefaults standardUserDefaults] synchronize])NSLog(@"Could Save");
    >
    > -registerDefaults: does not actually change default values, it simply sets what the default values should be in certain cases. You can read NSUserDefaultsController about ways to use default values. If you delete your plist preference file, then re-run, you might see different results. You might be happier in the long wrong if you follow the docs and have all of your application defaults in app delegate's +initialize, or as a pre-defined plist in your project. You also don't need to call -synchronize if your app will terminate normally (saving defaults is one of the exit functions).
    >
    Well I did as the Apple example I mentioned, they where doing that in the applicationDidFinishLaunch. Nevertheless,  so .. I have my .plist in the resources with all the values of the colors for the gradients, so from there is where Im reading to create the defaults gradients and I want to save them into the userDefaults,  then when the User click the preferences menu item he/she can choose different colors, then I will generate internally the new gradients  then use the [[NSUserDefaults standarUSerDefaults] setObjectForKey:kGradientKey]. If the user click the button restore defaults I will generate the defaults values form the .plist I have in the resources folder, and again set the gradient data.

    I deleted the com.mycompany.myapp, reloaded the application and no files are generated again, shall i run it on release mode?.. :S.

    So I will put this bunch of code on the +initialize, and Check the NSUserDefaultsController, so see what other ideas it will give me as you suggested.

    Thanks for the help

    Gustavo

    > HTH,
    >
    > Keary Suska
    > Esoteritech, Inc.
    > "Demystifying technology for your home or business"
    >
  • Ok I have finished with the defaults things, I think I got desperate after the first step which was setting the Application defaults, and I didn't realize I needed to complete the second step which is allow the user to set his/defaults, so  after I did so,, all was fine, the .plist in the library was created properly and all tis working like a  a charm.

    Thanks

    Gustavo

    On Mar 1, 2010, at 4:47 PM, Gustavo Pizano wrote:

    >
    > On Mar 1, 2010, at 4:07 PM, Keary Suska wrote:
    >
    >> On Mar 1, 2010, at 3:01 AM, Gustavo Pizano wrote:
    >>
    >>> [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
    >>> if([[NSUserDefaults standardUserDefaults] synchronize])NSLog(@"Could Save");
    >>
    >> -registerDefaults: does not actually change default values, it simply sets what the default values should be in certain cases. You can read NSUserDefaultsController about ways to use default values. If you delete your plist preference file, then re-run, you might see different results. You might be happier in the long wrong if you follow the docs and have all of your application defaults in app delegate's +initialize, or as a pre-defined plist in your project. You also don't need to call -synchronize if your app will terminate normally (saving defaults is one of the exit functions).
    >>
    > Well I did as the Apple example I mentioned, they where doing that in the applicationDidFinishLaunch. Nevertheless,  so .. I have my .plist in the resources with all the values of the colors for the gradients, so from there is where Im reading to create the defaults gradients and I want to save them into the userDefaults,  then when the User click the preferences menu item he/she can choose different colors, then I will generate internally the new gradients  then use the [[NSUserDefaults standarUSerDefaults] setObjectForKey:kGradientKey]. If the user click the button restore defaults I will generate the defaults values form the .plist I have in the resources folder, and again set the gradient data.
    >
    > I deleted the com.mycompany.myapp, reloaded the application and no files are generated again, shall i run it on release mode?.. :S.
    >
    > So I will put this bunch of code on the +initialize, and Check the NSUserDefaultsController, so see what other ideas it will give me as you suggested.
    >
    > Thanks for the help
    >
    > Gustavo
    >
    >
    >> HTH,
    >>
    >> Keary Suska
    >> Esoteritech, Inc.
    >> "Demystifying technology for your home or business"
    >>
    >
previous month march 2010 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today