 |
 |
When are NSDefaults written to disk?
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status:
Offline
|
|
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
|
|
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
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |