如何引用Obj-C代码中的环境变量 [英] How to reference a environment variable inside Obj-C code

查看:152
本文介绍了如何引用Obj-C代码中的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Xcode源代码树中定义了一个名为MY_SRC_DIR的路径变量。我想获取这个环境变量的值,并将其放在obj-c代码中的NSString中。例如,

   - (NSString *)getSourceDir 

{

return @$ {MY_SRC_DIR}; //不是正确的解决方案,这是问题

}


解决方案

http://rosettacode.org/wiki/Environment_variables#Objective -C



[[NSProcessInfo processInfo] environment] 返回当前环境的NSDictionary。例如:

  [[NSProcessInfo processInfo] environment] objectForKey:@MY_SRC_DIR] 


I define a path variable in Xcode source tree called "MY_SRC_DIR". I would like to get the value of this environment variable and put it in a NSString in the obj-c code. For example,

-(NSString*) getSourceDir

{

    return @"${MY_SRC_DIR}"; // not the right solution and this is the question

}

解决方案

From http://rosettacode.org/wiki/Environment_variables#Objective-C:

[[NSProcessInfo processInfo] environment] returns an NSDictionary of the current environment.

For example:

[[[NSProcessInfo processInfo] environment] objectForKey:@"MY_SRC_DIR"]

这篇关于如何引用Obj-C代码中的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆