PDA

View Full Version : Help with Aaron Hillegass Chapter 10 Challenge




HungrySeacow
Feb 16, 2006, 06:55 PM
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:@"BNREmptyDocKey"];
[defaults removeObjectForKey:@"BNRTableBgColorKey"];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
}



HexMonkey
Feb 16, 2006, 07:22 PM
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).