Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > When are NSDefaults written to disk?

When are NSDefaults written to disk?
Thread Tools
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
May 31, 2003, 09:49 PM
 
I have a command line app that uses defaults, but it doesn't seem to write them out. It is meant to function as a background daemon, so the defaults can be changed by entering arguments when it is run. This seems to work okay, but it never writes out the prefs file so the defaults aren't remembered the next time the app is run.

Code:
#import <Foundation/Foundation.h> #import <Machine.h> #import <FHMclientcontroller.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSString *logFile; NSString *secondLogFile; NSMutableDictionary *defaultPrefs=[NSMutableDictionary dictionary]; [defaultPrefs setObject:@"Not Entered" forKey:@"logFileDirectoryKey"]; [defaultPrefs setObject:@"Not Entered" forKey:@"secondLogFileDirectoryKey"]; [defaultPrefs setObject:[NSNumber numberWithInt:120] forKey:@"refreshTimeKey"]; [[NSUserDefaults standardUserDefaults] registerDefaults: defaultPrefs]; NSLog(@"args=%d",argc); if (argc==2) { NSLog(@"argv[2]=%s",argv[1]); logFile=[NSString stringWithCString:argv[1]]; NSLog(@"logfile done"); [[NSUserDefaults standardUserDefaults] setObject:logFile forKey:@"logFileDirectoryKey"]; NSLog(@"nsdefaults done"); [[NSNotificationCenter defaultCenter] postNotificationName:@"logFilesChanged" object:nil]; [logFile release]; } if (argc==3) { NSLog(@"args=2"); logFile=[NSString stringWithCString:argv[1]]; secondLogFile=[NSString stringWithCString:argv[2]]; [[NSUserDefaults standardUserDefaults] setObject:logFile forKey:@"logFileDirectoryKey"]; [[NSUserDefaults standardUserDefaults] setObject:secondLogFile forKey:@"secondLogFileDirectoryKey"]; [[NSNotificationCenter defaultCenter] postNotificationName:@"logFilesChanged" object:nil]; [logFile release]; [secondLogFile release]; } FHMclientcontroller *controller=[[FHMclientcontroller alloc] init]; while ( [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]] ); [pool release]; return 0; }

kman
     
Senior User
Join Date: Nov 2000
Status: Offline
Reply With Quote
May 31, 2003, 10:33 PM
 
And the lord high developer said from on high: "Read the documentation"

And the developer read the documentation that Apple provided, and found the discussion on the synchronize method of NSUserDefaults particularly exciting. The developer was careful, however, to make sure they read and understood the entire page, for they wished to be enlightened.



- proton
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:38 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2