I found the idiomatic answer for getting an environment variable, which I might generalize as:
NSString *key = @"<whatever>";
NSString *result;
result = [[[NSProcessInfo processInfo]environment]objectForKey:key];
But what's the idiomatic way for setting an environment variable? I see from the docs that the environment
dictionary is a read-only NSDictionary
object, rather than an NSMutableDictionary
, or some other special class that would allow me to do a setObject:forKey:
. (And indeed, if I coerce the types this way, it has no visible effect.)
I could use the standard UNIX (*nix) way:
char *key = "<whatever>", *value = "<something>";
int overwrite = 1;
setenv(key, value, overwrite);
And if that's what I need to do, that's what I'll do. I'm just wondering, is there an idiomatic objective-C way of doing this, that's different, and that I'm just failing to find? I like to do things in recommended ways where I can, which seems to me it would mean using an Objective-C class... if there is one for doing this. How do others do this?
(For what it's worth, my next step will be to then execute a program with the modified environment.)