How can I read a environmental variable in Cocoa?

前端 未结 2 909
故里飘歌
故里飘歌 2021-02-08 19:12

How could I read an environmental variable that a user has set?

I\'m new to desktop development on the Mac (cocoa), and I am building a little tool that I can use to acc

相关标签:
2条回答
  • 2021-02-08 19:29

    You can use a C API from the GNU library http://www.gnu.org/software/libc/manual/html_node/Environment-Access.html#Environment-Access

    converting to NSString: modern obj-c:

    NSString *envVarString = @(getenv("__MY_ENV_NAME__"));
    

    legacy obj-c:

    NSString *envVarString = [NSString stringWithUTF8String: getenv("__MY_ENV_NAME__")];
    
    0 讨论(0)
  • 2021-02-08 19:41

    Look at the environment method on a NSProcessInfo. It returns a NSDictionary of the environment so e.g. for PATH

    NSString* path = [[[NSProcessInfo processInfo]environment]objectForKey:@"PATH"];
    
    0 讨论(0)
提交回复
热议问题