Cocoa 获得系统环境变量

 这个类 NSProcessInfo 可以获取环境变量。

NSDictionary *envDic;

envDic = [[NSProcessInfo processInfo] environment];
    if([[envDic allKeys] containsObject:@"LOGNAME"] ){   // 判断是否存在环境变量 LOGNAME
        [mainTextField setStringValue:[envDic valueForKey:@"LOGNAME"] ] ; // 获得环境变量 LOGNAME
        [seconTtextField setStringValue:@"How are you"];
    }

 

ADC 中的参考文档如下:

 

Overview

The NSProcessInfo class provides methods to access information about the current process. Each process has a single, shared NSProcessInfo object, known as process information agent.

 

这个处理程序的代理包含:命令行参数,系统环境变量,主机名,进程名。

The process information agent can return such information as the arguments, environment variables, host name, or process name. The processInfo class method returns the shared agent for the current process—that is, the process whose object sent the message. For example, the following line returns the NSProcessInfo object, which then provides the name of the current process:

NSString *processName = [[NSProcessInfo processInfo] processName];
The NSProcessInfo class also includes the operatingSystem method, which returns an enum constant identifying the operating system on which the process is executing.

NSProcessInfo objects attempt to interpret environment variables and command-line arguments in the user's default C string encoding if they cannot be converted to Unicode as UTF-8 strings. If neither conversion works, these values are ignored by the NSProcessInfo object.

 

你可能感兴趣的:(Cocoa 获得系统环境变量)