Re: Access to argc & argv
Re: Access to argc & argv
- Subject: Re: Access to argc & argv
- From: Chris Parker <email@hidden>
- Date: Wed, 26 Oct 2005 13:11:05 -0700
On Oct 26, 2005, at 11:40 AM, George Lawrence Storm wrote:
Otherwise as I look back into the archives I see conflicting
recommendations as as to if I should access them through
NSUserDefaults or NSProcessInfo.
Hrm. I haven't looked back into the archives so I'm not sure what the
recommendations are, but my answer (as usual, perhaps?) would be, "It
depends." :)
NSUserDefaults will allow you to have easy access to things passed in
on the command line (and do some handy conversions for you),
*provided they're in the format NSUserDefaults expects*. You may
access command-line arguments through NSUserDefaults as long as
everything's passed on the command-line as "-someKey someValue".
A command line passed as
./myCocoaApp -checkForUpdates YES -hostname www.mydomain.com -port 4621
is well-suited for picking apart with something like this (typed into
Mail):
NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
NSString * shouldCheckForUpdates = [defs
boolForKey:@"checkForUpdates"];
NSString * host = [defs stringForKey:@"hostname"];
int port = [defs integerForKey:@"port"];
However, NSUserDefaults does not do "implicit YES for presence of a
key". So if you're trying to do something on the command line like:
./myCocoaApp -checkForUpdates -hostname www.somehost.com
You could still ask the NSUserDefaults instance for "hostname" but if
you were to try to get a bool for the "checkForUpdates" key it would
come back with "NO".
For this kind of more sophisticated argument parsing, you'd have to
check [[NSProcessInfo processInfo] arguments] for the -
checkForUpdates argument in the list.
.chris
--
Chris Parker
Cocoa Frameworks
Apple Computer, Inc.
_______________________________________________
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