Help with Aaron Hillegass Chapter 10 Challenge

Discussion in 'Mac Programming' started by HungrySeacow, Feb 16, 2006.

  1. HungrySeacow macrumors regular

    Jan 11, 2006
    West Palm Beach
    I am guessing a few people have read his book and might be able to help me out with this challenge. For those who have not read the book, the challenge is to add a button to a preference panel to reset the values in the NSUserDefaults standardUserDefaults "BNREmptyDocKey" and "BNRTableBgColorKey" keys. One is a stores data for a collorWell, and the other for a checkbox. This is the code that I came up with, that just doesn't work :(. I get a warning, "Warning: passing argument 1 of 'registerDefaults:' from distinct Objective-C type". Anyone have an idea how this is supposed to work?

    - (IBAction)resetDefaults:(id)sender
    NSUserDefaults *defaults;
    defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:mad:"BNREmptyDocKey"];
    [defaults removeObjectForKey:mad:"BNRTableBgColorKey"];
    [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
  2. HexMonkey Administrator


    Staff Member

    Feb 5, 2004
    New Zealand
    registerDefaults: takes an NSDictionary parameter, but defaults is an NSUserDefaults object, so you get the warning. The registerDefaults: message isn't needed anyway, so if you remove that line the code should work.

    The other thing you might want to take into account is that the preferences file isn't written immediately when you make changes to the standard user defaults object. The file is updated periodically, but if you quit before it's updated the changes will not take effect. To be safe you could include the code
    [defaults synchronize];
    at the end of the method (or at some later time before the application is quit).

Share This Page