NSUserDefaults - When is it written to disk?
NSUserDefaults - When is it written to disk?
- Subject: NSUserDefaults - When is it written to disk?
- From: Jeremy French <email@hidden>
- Date: Fri, 3 Dec 2004 09:17:36 -0500
When are UserDefaults written to the plist document?
In an an example (from "Learning Cocoawith Objective-C" second edition,
pages 290-293), no default is actually written to disk until the user
has tabbed into a field displaying a default value: then that default
value is written when the application quits.
Why aren't all of the default values written to disk the first time the
application opens?
The example dispalys several default values in text fields. There are
three methods: init (which sets up the intial user defaults);
awakeFromNib (which assigns default values to the text fields); and
textFieldChanged (which updates user prefs).
Three questions:
* Why doesn't the act of simply opening the application and then
quitting it create the preference file?
* Why is the preference file created only after tabbing into a
preference field and then quitting the application?
* Why is a specific preference added to the preference file only after
tabbing into its field?
Here are some additional details:
=============
Interface
=============
/* Controller */
#import <Cocoa/Cocoa.h>
@interface Controller : NSObject
{
IBOutlet NSTextField *bookField; // Preference favorite book
IBOutlet NSTextField *cityField; // Preference favorite city
IBOutlet NSTextField *colorField; // Preference favorite color
IBOutlet NSTextField *foodField; // Preference favorite food
NSUserDefaults * prefs; // reference to user defaults
}
- (IBAction)textFieldChanged:(id)sender;
@end
=============
Implementation
=============
#import "Controller.h"
@implementation Controller
- (id)init
{
[super init];
NSMutableDictionary * defaultPrefs = [NSMutableDictionary dictionary];
[defaultPrefs setObject:@"Learning Cocoa" forKey:@"FavBook"];
[defaultPrefs setObject:@"San Francisco" forKey:@"FavCity"];
[defaultPrefs setObject:@"Red" forKey:@"FavColor"];
[defaultPrefs setObject:@"Mexican" forKey:@"FavFood"];
prefs = [[NSUserDefaults standardUserDefaults] retain];
[prefs registerDefaults:defaultPrefs];
[defaultPrefs setObject:@"Learning Cocoa" forKey:@"FavBook"];
[defaultPrefs setObject:@"Learning Cocoa" forKey:@"FavBook"];
[defaultPrefs setObject:@"Mexican" forKey:@"FavFood"];
return self;}
- (IBAction)textFieldChanged:(id)sender
{
if (sender == bookField)
{
[prefs setObject:[bookField stringValue] forKey:@"FavBook"];
}
else if (sender == cityField)
{
[prefs setObject:[cityField stringValue] forKey:@"FavCity"];
}
else if (sender == colorField)
{
[prefs setObject:[colorField stringValue] forKey:@"FavColor"];
}
else if (sender == foodField)
{
[prefs setObject:[foodField stringValue] forKey:@"FavFood"];
}
}
- (void)dealloc
{
[prefs release];
[super dealloc];
}
- (void)awakeFromNib
{
[bookField setStringValue:[prefs stringForKey:@"FavBook"]];
[cityField setStringValue:[prefs stringForKey:@"FavCity"]];
[colorField setStringValue:[prefs stringForKey:@"FavColor"]];
[foodField setStringValue:[prefs stringForKey:@"FavFood"]];
}
@end
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden