Re: storing default preferences
Re: storing default preferences
- Subject: Re: storing default preferences
- From: Bjoern Kriews <email@hidden>
- Date: Mon, 15 Mar 2004 21:17:47 +0100
On 14.03.2004, at 07:21, cauri wrote:
New to cocoa programming.
Hi all,
My application has a number of lists that users can customise. I
manage these lists through the preferences panel. On first run (or on
the click of a "restore defaults" button) I would like these arrays to
populate with the default data. What is the best way to store this
data? In an XML file, a tabbed text file, or does some other mechanism
exist in cocoa? And what is the most efficient way to retrieve this
data?
Here are some code fragments I use (cut from multiple files and
adjusted for this posting,
no testing done).
I have a factoryDefaults.plist in my Resources section in XCode (make
sure it is checked
so it will be included in builds) - this plist resembles those that are
written to
Library/Preferences. An example is attached below.
The dictionary returned by dictionaryFromPlistResource: can be given to
NSUserDefaultControllers setInitialValues: method or the equivalent in
NSUserDefaults.
Feel free to email me if you have further questions.
Regards, Bjoern
- (id) dictionaryFromPlistResource: (NSString *) resource {
NSDictionary *dict;
if( ! ( dict = [NSDictionary dictionaryWithContentsOfFile:
[self pathForMandatoryResource: resource ofType: @"plist"] ] ) )
[self crashWithMessage: [NSString stringWithFormat: @"can't
load dictionary-resource '%@'", resource]];
return dict;
}
- (NSString *) pathForMandatoryResource: (NSString *) resource ofType:
(NSString *) type {
NSString *path;
if(! (path = [[NSBundle bundleForClass: [self class]]
pathForResource: resource ofType: type]))
[self crashWithMessage: [NSString stringWithFormat: @"can't
locate resource %@ of type %@", resource, type]];
return path;
}
- (void) crashWithMessage: (NSString *) message
{
// well some work to do to live up to the name
NSRunCriticalAlertPanel(@"Houston, we got a problem here...",
message,
@"Sorry",
nil,
nil);
[NSApp terminate: self];
}
-----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"
http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>HistoryWindowVisible</key>
<false/>
<key>RowNumberRulerEnabled</key>
<true/>
<key>XMLRPCURL</key>
<string>
http://127.0.0.1/cgi-bin/simpleRPC.pl</string>
<key>XMLRPCMethodName</key>
<string>Scratch.dbi</string>
<key>XMLRPCUserName</key>
<string></string>
<key>XMLRPCPassword</key>
<string></string>
</dict>
</plist>
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.